Published on

Regex for hashtag match

Regex for hashtag match

A hashtag is a word or phrase preceded by a hash symbol (#), used on social media platforms to identify messages on a specific topic. The hashtag is used to group messages or content together, and allows users to easily search for and find specific types of content.

For example, if someone wanted to find all of the tweets related to a particular event, they could search for a hashtag related to that event. Hashtags are used on a variety of social media platforms, including Twitter, Instagram, and Facebook, and can be used to promote events, campaigns, or products, or to facilitate discussions on a particular topic. To use a hashtag, a user simply includes it in their post, tweet, or other content, and other users can then search for that hashtag to see all of the related content. In this article let's understand how we can create a regex for extracting hashtags from a string and how regex can be matched for hashtags.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of Hashtags

  • It should start with a #
  • It can be followed by any number of characters
  • It can also be accompanied by text/string
  • It can exist independently as #string

Regex for matching and extracting hashtags from a string

Regular Expression-

Containing minimum 8 characters, with at least 1 letter and 1 number-


Test string examples for the above regex-

Input StringMatch Output
get work donedoes not match
we rockdoes not match
#hello how are you?matches

Here is a detailed explanation of the above regex-


# matches the character # with index 3510 (2316 or 438) literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
g modifier: global. All matches (don't return after first match)

Hope this article was useful to matching and extracting hashtags from string using regex pattern.