views:

70

answers:

1
+5  Q: 

regexp exclusion

Hello everyone! 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?

+4  A: 

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'
:)
:-)
polygenelubricants
I've added example in Perl.
J.F. Sebastian
Oh yeah, you must be regexp guru. It works fine, thank you!
Vlad