Page 1 of 1

RegExp Three-Way Negative Lookbehind Assertion

Posted: Fri May 05, 2006 5:05 pm
by tomprogers
I'm trying to write a regexp that will match any \n that is not immediately preceeded by one of three pieces of text. I tried this:

Code: Select all

'/(?<!(one|two|three))\n/i'
but I ran into that problem where all the branches are not of equal length (which is actually the case in my situation).

I also tried:

Code: Select all

'/(?<!one)(?<!two)(?<!three)\n/i
It didn't work, as expected.

Any ideas?

Posted: Fri May 05, 2006 6:50 pm
by Buddha443556
Might try adding an 's' flag to treat it as string.

Code: Select all

/(?<!=one)(?<!=two)(?<!=three)\n/is
Have you tried?

Code: Select all

/((?<!=one)|(?<!=two)|(?<!=three))\n/is
Just guessing here... :oops:

EDIT: Notice the equal sign too.

Posted: Sat May 06, 2006 2:44 am
by s.dot
You may, however, wish to treat a string as a multi-line buffer, such that the "^" will match after any newline within the string, and "$" will match before any newline. At the cost of a little more overhead, you can do this by using the /m modifier on the pattern match operator

Posted: Sat May 06, 2006 7:12 am
by Chris Corbyn
I should just point out that a negative lookbehind has to be a fixed string.... technically it's called fixed-width, so I'm not sure... perhaps the use of the "alternative" operator is permitted.

Code: Select all

/(?:(?<!one)\n)|(?:(?<!two)\n)|(?:(?<!three)\n)/i

Posted: Tue May 09, 2006 2:34 pm
by tomprogers
scottayy wrote:You may, however, wish to treat a string as a multi-line buffer, such that the "^" will match after any newline within the string, and "$" will match before any newline. At the cost of a little more overhead, you can do this by using the /m modifier on the pattern match operator
I'm interested in replacing the line breaks, so it's important to me that the preg_replace consume the \n, hence my decision to not use /m. I do use /m in the expression immediately preceeding the one I'm having trouble with:

Code: Select all

preg_replace('/^\*(.*?)$/im', '<li>$1</li>', $str);