lookbehind assertion is not fixed length

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

lookbehind assertion is not fixed length

Post by bokehman »

Code: Select all

$regex = '/(?<=[^\\\\])/';

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?
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post 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?
Last edited by shoebappa on Thu Aug 03, 2006 8:24 pm, edited 1 time in total.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post 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
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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

Code: Select all

(?<!\\\\)
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
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
Post Reply