ThomasReggi
ThomasReggi

Reputation: 59365

Regex hashtag & html entity mixup

I have the regex expression #(\w+) to catch a hashtag like #wine in a string and it also happens to pickup on html entities like '. I'd need it to avoid these by looking for the ampersand. How can I validate this?

Upvotes: 2

Views: 431

Answers (2)

Narendra Yadala
Narendra Yadala

Reputation: 9664

You can try a regex like this (?:^|\s)(#\w+) and pick the first captured group as the match.

Upvotes: 0

davidchambers
davidchambers

Reputation: 24806

I suggest using a negative lookbehind assertion if your language's regex engine supports them:

(?<!&)#(\w+)

Failing that, this would do the trick (although somewhat less elegantly):

(?:^|[^&])#(\w+)

Upvotes: 5

Related Questions