Reputation: 15
I am doing a preg_replace
to turn hashtags to urls using the following RegEx:
#([^0-9_\s\W][\p{L}0-9]{2,})
The problem is that url fragment identifiers are also getting included. How can I exclude them by improving this RegEx? Also every line may or many not be within <p>
tags.
I appreciate your response.
Upvotes: 1
Views: 287
Reputation: 4259
This one made the work:
(?<=\W)#(\w+)
http://www.phpliveregex.com/p/b1u
Upvotes: 1
Reputation: 51330
I believe you want to exclude text like this from the matches:
http://stackoverflow.com/questions/29996848#question-header
\______________/
A simple solution would be to require a whitespace character before the #
, which is easily done with a lookbehind:
(?<=\s|^)#([^\d_\s\W][\p{L}\d]{2,})
Demo.
Upvotes: 0