Page 1 of 1
Not preceded by certain character if not at beginning of str
Posted: Sun Mar 11, 2007 5:38 am
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

Posted: Sun Mar 11, 2007 8:48 am
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".
Posted: Mon Mar 12, 2007 8:27 am
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?
Posted: Mon Mar 12, 2007 9:29 am
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.
Posted: Mon Mar 12, 2007 9:48 am
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!