Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp exclusion

I have regexp to change smileys to images. Here it is

(?:(?![0]:\)|:\)\)|:-\)\)))(:\)|:-\))

The point is not to change 0:) and :)) and :-)) while changing :) and :-) It works pretty well with :)) and :-)) but somehow still grabs :) in 0:)

Where's my mistake?

like image 933
Vlad Avatar asked Mar 20 '10 12:03

Vlad


1 Answers

So you want to match :) and :-), but they must not be preceded by 0 or followed by another )? Then this is the pattern:

(?<!0):-?\)(?!\))

Basically it's

(?<!0) : negative lookbehind; must not be preceded by 0
:-?\)  : smiley with optional nose
(?!\)) : negative lookforward; must not be followed by )

Example:

$ echo ':) :-) ok 0:) :)) :-)) 0:-)) 0:-) : )' | \
> perl -lne'print $1 while /(?<!0)(:-?\))(?!\))/g'
:)
:-)
like image 65
polygenelubricants Avatar answered Sep 21 '22 01:09

polygenelubricants