regexp

John Macdonald john-Z7w/En0MP3xWk0Htik3J/w at public.gmane.org
Sun Apr 25 19:17:05 UTC 2004


On Sun, Apr 25, 2004 at 08:53:55PM +0300, Peter L. Peres wrote:
> 
> On Sat, 24 Apr 2004, John Macdonald wrote:
> 
> > could be written as:
> >
> > 	if ( /=<(?!foo\@bar\.baz)([^>]*)>/ )  {
> > 		print "got it = (\Q$1\E)";
> > 	}
> >
> 
> Ok, thank you very much. I was not so clear about what happens to the
> chunk that matches the ?! part.

If the (?!foo\@bar\,baz) can match (i.e. after
the already matched "<" you find the string
"foo-o+HKXD+gRuM at public.gmane.org"), the pattern fails at this point
with the usual backtracking (this "<" fails, this
"=", search through the string for another "=" to
try again).  If there was not "foo-o+HKXD+gRuM at public.gmane.org" found,
the (?!...) would have succeeded, and the regex
match would continue, still positioned after the
"=<", and look for a match of "([^>]*)>".

The key thing about a lookahead is that it is an
assertion - which means that while it can fail or
succeed, if it succeeds it does not match any actual
text, and the subsequent pattern element starts
searching from the same position.  Regexps have
two assertions "^" and "$", which match a position
without matching any characters.  Perl has added
a number of additional assertions.  As well as the
lookahead assertions (?!...) and (?=...), there are
boundary assertions like \b (which matches a "word
boundary").

-- 
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list