My PHP code looks like this:
$input = "City.name = 'New York'"; $literal_pattern = '/\'.[^\']*\'/'; preg_match($literal_pattern, $input, $token); echo $token[0]; // prints 'New York'
My regex needs to grab literals with escaped single quotes like:
$input = "City.name = 'New \' York'"; $literal_pattern = ???????????; preg_match($literal_pattern, $input, $token); echo $token[0]; // should prints 'New \' York'
What wil be the reges for $literal_pattern ?
Without this condition, simple…
…would suffice, of course: match all sequences of “single quote, followed by any number of non-single-quote symbols, followed by a single quote again”.
But as we need to be ready for two things here – both “normal” and “escaped” ones. So we should add some spice to our pattern:
It might look odd (and it is), but it’s actually pretty simple too: match sequences of…
'or\),Example: