user3833490
user3833490

Reputation: 15

turning hashtags to link but ignoring the urls fragment Identifiers

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

Answers (2)

Rodrigo L&#243;pez
Rodrigo L&#243;pez

Reputation: 4259

This one made the work:

(?<=\W)#(\w+)

http://www.phpliveregex.com/p/b1u

Upvotes: 1

Lucas Trzesniewski
Lucas Trzesniewski

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

Related Questions