hi,
i'm looking for a regex that adheres to the following specification, but i'm not sure if it's possible to do such a selection. any help would be greatly appreciated.
i want to replace all occurences of a comma with the phrase "COMMA_DELIMITER", if and only if the comma is located within quotes.
examples:
tom said "hello, world"
becomes
tom said "helloCOMMA_DELIMITER world"
but
tom said, "hello world"
would stay the same.
is this possible?
the following regex would replace an entire quoted string with COMMA_DELIMITER:
$sentence = preg_replace("/(\"[^\"]*\")|('[^']*')/", "COMMA_DELIMITER", $sentence);
is there a way to tweak the above (or just a better way in general!) to replace a comma, if and only if it is located within quotes?
i guess an english equivalent to the problem would be
if ((there's an opening quote and an aribitrary number of characters before a comma)
&& (there's an arbitrary number of characters and a closing quote after the comma))
{
replace the comma with COMMA_DELIMITER
}
thank you!