View Full Version : help with a regular expression please
akanevsky
12-04-2005, 02:14 PM
Say I have a string:
x!!x bbbbbdddd x!!x bbbbaaaaddddd x!!x bbbbcccccddddd
How do I match the bbbb(.*)dddd that do NOT have an "x!!x" substring between them? Don't ask what for, just tell me how do to such regex. explode() is not an option for my purposes. Thanks.
Ideally, it would be something like ([^x!!x]*), but that won't work for some reason.
Andreas
12-04-2005, 02:36 PM
lookahead/lookbehind assertions
akanevsky
12-04-2005, 02:45 PM
Example plz?
Andreas
12-04-2005, 02:50 PM
> x!!x bbbbbdddd x!!x bbbbaaaaddddd x!!x bbbbcccccddddd
From this string, what would be your desired match(es)?
akanevsky
12-04-2005, 08:06 PM
Hmm..
I just want to end up having bbbb moved as towards the closest ddddd, so that the result would be:
x!!x bbbbdddd x!!x aaaabbbbddddd x!!x cccccbbbbddddd
EDIT: Actually, nevermind. That won't work as desired because the corresponding bbbb and dddd could have inner pairs of x!!x between them, which would give an undesired result. So nvm... :P
Andreas
12-04-2005, 09:32 PM
preg_replace('/bbbb(.*)dddd/U', '$1bbbbdddd', 'x!!x bbbbbdddd x!!x bbbbaaaaddddd x!!x bbbbcccccddddd');
?
akanevsky
12-04-2005, 09:41 PM
Yes, that works. However, as I said, it could happen like.. let me explain.
<bbbb <bbbbaaaadddd> dddd>
So here, my result would have to be:
< <aaaabbbbdddd> bbbbdddd>
However, because there is another <....> inside the outer <....>, there is no way to see the the first bbbb belongs to the latest dddd. Or is there? :P
Andreas
12-04-2005, 10:04 PM
<Disclaimer>
This Code is utterly bad
</Disclaimer>
function recurse_replace($str)
{
if (preg_match('/bbbb(.*)dddd/', $str))
{
return preg_replace('/bbbb(.*)dddd/eS', "recurse_replace('\\1') . 'bbbbdddd'", $str);
}
else
{
return $str;
}
}
akanevsky
12-04-2005, 10:15 PM
Why is it utterly bad? Because of performance?
Andreas
12-04-2005, 10:24 PM
Because it is recursive and not checked ;)
akanevsky
12-04-2005, 10:43 PM
Oh. It won't work ;)
Imagine a <bbbb <bbbbaaaadddd> dddd> <bbbb <bbbbaaaadddd> dddd>
then I would end up with
< <aaaabbbbdddd> dddd> < <aaaabbbbdddd> bbbbbbbbdddd>
instead of
< <aaaabbbbdddd> bbbbdddd> < <aaaabbbbdddd> bbbbdddd>
which ain't no good :P
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.