How do I match a quoted string within quotes?
Using Python with source string literals to ensure no further interpretation of escape sequences works, the following option works:
import re
x = re.compile(r'^"((?:[^"\\]|\\.)*)"')
s = r'"quote\_with\\escaped\"characters" more"'
mo = x.match(s)
print mo.group()
emits "quote\_with\\escaped\"characters"
; I believe that in your version (which will also abort the game early if replaced here) the subexpression "not double" ( [^"]
) will swallow the backslashes you intend to take as escaping immediately following characters. All I'm doing here is that backslashes like this are NOT swallowed this way, and as I said, they seem to work with this change.
source to share
Not going to be confused, just different information that I played with. Below regexp (PCRE) try not to match incorrect syntax (eg end with \ ") and can be used with either" or "
/('|").*\\\1.*?[^\\]\1/
for use with php
<?php if (preg_match('/(\'|").*\\\\\1.*?[^\\\\]\1/', $subject)) return true; ?>
For:
"quote\_with\\escaped\"characters" "aaa"
'just \'another\' quote "example\"'
"Wrong syntax \"
"No escapes, no match here"
This is the only match:
"quote\_with\\escaped\"characters" and
'just \'another\' quote "example\"'
source to share