Not preceded by certain character if not at beginning of str

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

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Not preceded by certain character if not at beginning of str

Post by Dave2000 »

I wish to match the string "http", and, if "http" is not the beginning at the string, cases where the "http" is not preceded by an "a".

So far I have...

Code: Select all

$string = preg_replace('/^http/', 'true', $string);

$string = preg_replace('/[^a]http/', 'true', $string);
but i dont know how to do combine them. :? (I need to combine them because this is part of a bigger regex.)

Hope it makes sense ;) Thank you

Shears :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

[feyd@home]>php -r "$a = 'http had a kid named ahttp, followed by http jr'; var_dump(preg_split('#(?<!^|a)http#', $a));"
array(2) {
  [0]=>
  string(40) "http had a kid named ahttp, followed by "
  [1]=>
  string(3) " jr"
}
Find "http" not at beginning of strings nor when preceded by "a".
User avatar
veridicus
Forum Commoner
Posts: 86
Joined: Fri Feb 23, 2007 9:16 am

Post by veridicus »

Wow. I'd like to understand how this one works.

I see how ^|a catches the beginning of string or "a", but what exactly does ?<! do? That somehow negates the rest of the subset in the parentheses?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

(?<! ... ) is what is called a negative, look-behind, zero-width assertion. The expression contained will cause the rest of the expression to not match if the text prior to that point matches it. If you remove the exclamation, it becomes a positive, look-behind.

The nice thing about zero-width assertions is that they are not included in the final match, only understood through the course of parsing it.
User avatar
veridicus
Forum Commoner
Posts: 86
Joined: Fri Feb 23, 2007 9:16 am

Post by veridicus »

I'm pretty good with regular expressions, but you just blew my mind. That might be the most useful tip I've heard in months. Thanks!
Post Reply