Page 1 of 1
lookbehind assertion is not fixed length
Posted: Thu Aug 03, 2006 7:13 pm
by bokehman
Code: Select all
Warning: preg_replace() [function.preg-replace]: Compilation failed: lookbehind assertion is not fixed length at offset 9 in...
How is that not fixed length?
Posted: Thu Aug 03, 2006 8:15 pm
by shoebappa
I'm still new to lookahead and behind but it seems to me that rather than using a positive lookbehind for a negative pattern, you should just use a negative lookbehind?
The other problem is you're not saying what to look for that isn't preceeded by \\
Usually I think you would do something like:
Code: Select all
$pattern = "/(?<!\\\\)something/";
Could you explain what you're trying to match?
Posted: Thu Aug 03, 2006 8:19 pm
by shoebappa
I found this to be pretty good, I had to refer to it and play with some code and repeat several times before I understood, and I still haven't used them enough to say I'm comfortable with look arounds.
http://www.regular-expressions.info/lookaround.html
Posted: Thu Aug 03, 2006 8:36 pm
by bokehman
shoebappa wrote:I'm still new to lookahead and behind but it seems to me that rather than using a positive lookbehind for a negative pattern, you should just use a negative lookbehind?
I've narrowed it down to the negated class as well and found
works fine.
shoebappa wrote:The other problem is you're not saying what to look for that isn't preceeded by \\
Zero width assertions are completely valid as standalone expressions as demonstrated here:
Code: Select all
echo preg_replace('/(?<=3)/', "4", '1235'); // 12345
Posted: Thu Aug 03, 2006 8:46 pm
by shoebappa
Word, didn't know that... But had seen the term zero width assertions, just never knew how they'd be useful. I guess it works like a place holder, wouldn't be useful for matching something since the match is empty.
Posted: Thu Aug 03, 2006 9:01 pm
by bokehman
It can be handy in conjunction with preg_split if you want to break a string into chunks but not consume the characters you were splitting on.