I am working with regular expressions for years now but this time I am struggling hard.
Assuming I have the following simple string.
$content = "foo 'bar \' [\'],' test";
Now I want to match everything within single quotation marks. There are even escaped characters in it. With my current regex those escaped characters are blowing up the results.
$regex = "/\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'/";
The results
// expected
[0] => "'bar \' [\'],'"
// current
[0] => "'],'"
Live demo HERE .
You want here to find an open quote and its closing one, so no escaped quote.
(?<!\\\\)'.*?(?<!\\\\)'
will do so
Explanation :
(?<!
negative lookbehind
\\\\)
escaped backslash and closing lookbehind
'
the quote which has not been escaped (the negative look behind has checked it)
.*?
any char : .*
in lazy mode : ?
so the next quote will be evaluate
(?<!\\\\)
again negative lookbehind to check if the quote has been escaped
'
Final not escaped quote
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.