Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to match #hashtag but not #hashtag; (with semicolon)

Tags:

regex

hashtag

I have the current regular expression:

/(?<=[\s>]|^)#(\w*[A-Za-z_]+\w*)/g

Which I'm testing against the string:

Here's a #hashtag and here is #not_a_tag; which should be different. Also testing: Mid#hash. #123 #!@£ and <p>#hash</p>

For my purposes there should only be two hashtags detected in this string. I'm wondering how to alter the expression such that it doesn't match hashtags that end with a ; in my example this is #not_a_tag;

Cheers.

like image 593
Wex Avatar asked Jul 21 '16 14:07

Wex


2 Answers

How about the following:

\B(\#[a-zA-Z]+\b)(?!;)

Regex Demo

  • \B -> Not a word boundary
  • (#[a-zA-Z]+\b) -> Capturing Group beginning with # followed by any number of a-z or A-Z with a word boundary at the end
  • (?!;) -> Not followed by ;
like image 158
tk78 Avatar answered Sep 22 '22 11:09

tk78


This is the best practice.

(#+[a-zA-Z0-9(_)]{1,})
like image 31
nhCoder Avatar answered Sep 20 '22 11:09

nhCoder