RegExp Three-Way Negative Lookbehind Assertion

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

Moderator: General Moderators

Post Reply
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

RegExp Three-Way Negative Lookbehind Assertion

Post 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?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post 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);
Post Reply