How do I match a quoted string within quotes?

/^"((?:[^"]|\\.)*)"/

      

Against this line:

"quote\_with\\escaped\"characters" more

      

It corresponds only \"

, even though I clearly defined \

as the escape-character (and it corresponds to \_

, and \\

fine ...).

+2


source to share


3 answers


It works correctly if you reverse the order of your two alternatives:

/^"((?:\\.|[^"])*)"/

      



The problem is that otherwise the important character \

gets eaten before it tries to match \"

. It worked before for \\

and \_

only because both characters in each pair match yours [^"]

.

+4


source


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.

0


source


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\"'

      

0


source







All Articles