Regex works without, but not with lookbehind assertion
Posted: Thu Jan 08, 2009 6:42 am
Hi, I try to find all occurences of ", " thats comma followed by a whitespace which are in between {...}. E.g.
"I like your {car, house, cat, forum} a lot, but I don't like commas in parentheses.
It would be easy find and replace if there weren't any commas outside of the brackets which I don't want to find (and replace). I tried this PHP script with regex and it finds me "{car, " what is correct.
If I try to look behind the "{car" and only get the ", " with this little change
I dont get any results anymore. Would anybody help me out and tell me whats wrong with my lookbehind assertion? Thank you very much!
"I like your {car, house, cat, forum} a lot, but I don't like commas in parentheses.
It would be easy find and replace if there weren't any commas outside of the brackets which I don't want to find (and replace). I tried this PHP script with regex and it finds me "{car, " what is correct.
Code: Select all
$lines = file ('test.txt');
foreach ($lines as $line_num => $line) {
$suchmuster = '#{[\w\s]+,\s#'; //'#(?<={[\w\s]+),\s(?=\w+|})#';
$ersetzung = '|';
//echo preg_replace($suchmuster, $ersetzung, $line);
preg_match($suchmuster, $line, $treffer);
echo $treffer[0]."<br>";
}Code: Select all
$lines = file ('test.txt');
foreach ($lines as $line_num => $line) {
$suchmuster = '#{(?<=[\w\s]+),\s#'; //'#(?<={[\w\s]+),\s(?=\w+|})#';
$ersetzung = '|';
//echo preg_replace($suchmuster, $ersetzung, $line);
preg_match($suchmuster, $line, $treffer);
echo $treffer[0]."<br>";
}