[thelist] Reg Ex - everything except a phrase

Kelly Hallman khallman at wrack.org
Wed Sep 10 13:10:35 CDT 2003


On Wed, 10 Sep 2003, Joshua Olson wrote:
> You da man!  I'm not familiar with Perl syntax... could you do me a favor
> and explain what this line does:
> 
> $d =~ s/^(.*)(?:fred)(.*)$/$1$2/ while $d =~ /fred/; print $d, "\n";

(?:xxxxx) is a positive lookahead assertion. This is an extended regex 
feature that you may not find in many regex engines that don't use PCRE 
(perl-compatible regular expressions, such as preg_ functions in PHP).

Originally I tried to use a lookahead assertion, but I couldn't figure one
that did what was requested, which was to match anything that did not
contain a certain sequence of characters. Indeed, the above regex matches
the sequence of characters, which was not the problem. No need for a
lookahead assertion if you simply want to remove a sequence of characters.
As someone said, the regex above is equivalent to $d =~ s/fred//g;

There is also a negative lookahead assertion (?!xxxxx) which "matches if
the expression wouldn't match xxxxx next". I also tried that, and couldn't
get the results I wanted...but it may contain a key to this problem...

-- 
Kelly Hallman
http://wrack.org/



More information about the thelist mailing list