Re: preg weirdness?

From: Date: Fri, 28 May 1999 13:49:02 +0000
Subject: Re: preg weirdness?
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-6154@lists.php.net to get a copy of this message
Rasmus Lerdorf wrote: > regex expected got > /.*/ X XX <== > /.+/ X X > // <error> XaXbXcX <== > /\w/ XXX XXX > /^\w+$/ X X > /\b/ XabcX XaXbXc <== > /^\w*?/ Xabc Xabc > /^\w+?/ Xbc Xbc > > The first one just looks wrong to me. But the behaviour is the same in > ereg_replace, so I guess it is correct. I took replacement algorithm from ereg_replace, so they do work the same. However, why it happens is a fallacy of the algorithm. The way it works is after a match is found and replaced, it advances to the point right after the end of the last match. So, in this case after first X is put in, the offset now puts it at the very end of the string and since .* will match an empty string, it puts another X there. > I suppose the second surprising one can be explained. It is just sort of > funny to think about though. There is exactly 1 nothing before and after > every char in the string? Here preg_replace() differs from > ereg_replace(). ereg_replace() will throw an error. That's how Perl works, I checked. > And the third weird one looks like a bug to me. \b is supposed to be a > word boundary. That is, it should match the transition from a \W to a \w > and vice versa. I only see two word boundaries like that in the "abc" > string. The actual output makes no sense unless the pcre library doesn't > understand \b being a word boundary. Same reason for this as for #1. \b matches at the beginning of the string, and since we advance to the end of the match and run it again it matches again. I will have to wade through Perl's replace code to see if this can be done better. -Andrey * Software never has bugs. It just develops random features. * -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#6154) next »