Reputation: 59365
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
Reputation: 9664
You can try a regex like this (?:^|\s)(#\w+)
and pick the first captured group as the match.
Upvotes: 0
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