Regex: requires quotes to be escaped in a string

Thank you for watching,

I had a terrible time trying to find the right search terms for this question with regular expression. I need to make sure that the quotes are already escaped in the string, otherwise the match will fail. (Most of the search results on this subject are simply pages where you need to avoid quotes or how to avoid quotes.)

Really:

This is valid This \"is Valid This is al\"so Valid\" 

Invalid:

 This i"s invalid This i"s inv"alid 

The only thing I managed to find is

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

It looks like the first part of the next, but nothing after a hidden quote

 This is a \"test 

Again, this should fail:

 This is a \"test of " the emergency broadcast system 

Thanks for any help, I hope this is even possible.

+6
source share
5 answers

In C #, this works the way you want:

 string pattern = "^([^\"\\\\]*(\\\\.)?)*$"; 

Disabling shielding leaves you with:

 ^([^"\\]*(\\.)?)*$ 

which roughly translates to: start-of-string, (multi-chars-exclusion-quote-or-backslash, optional-backslash-anychar) -repeated, end-of-string

These are markers of the beginning of a line and the end of a line that force a match over the full text.

+6
source

I don’t know which language you use, but I would do it as follows:

create a regular expression that matches the quote without a backslash, which will not happen when

 This is a \"test 

and succeed on

 This is a \"test of " the emergency broadcast system 

for example this one:

 .*(?<!\\)".* 

and then will use a negative expression with the result. hope this helps you

my test in java looks like

  String pat = ".*(?<!\\\\)\".*"; String s = "This is a \\\"test"; System.out.println(!s.matches(pat)); s = "This is a \\\"test of \" the emergency broadcast system"; System.out.println(!s.matches(pat)); 
+2
source

You want to use a negative lookbehind.

 (?<!\\)" 

This regular expression will match all quotation marks that are not preceded by a single slash.

If you run this regular expression against your sample string and find one or more matches, the string is invalid.

+2
source

You need to take everything except the backslash and quote, or the backslash and the next character.

 ([^\\"]|\\.)* 

Thus, this will not be done:

 ab\\"c 

This will be successful:

 ab\\\"c 

This will be successful:

 ab\"c 
+1
source

The RegEx you are looking for is:

 /^(?:[^"]*(?:(?<=\\\)"|))*$/ 

Explanation: [^"]* will match the input until the first one is found " or the end of the input is reached. If " found, make sure that in (?<=\\\)" lookbehind is always preceded by / . The above script repeats recursively until the end of input is reached.

TEST: Consider the following PHP code to verify:

 $arr=array('This is valid', 'This \"is Valid', 'This is al\"so Valid\"', 'This i"s invalid', 'This i"s inv"alid', 'This is a \"test', 'This is a \"test of " the emergency broadcast system - invalid'); foreach ($arr as $a) { echo "$a => "; if (preg_match('/^(?:[^"]*(?:(?<=\\\)"|))*$/', $a, $m)) echo "matched [$m[0]]\n"; else echo "didn't match\n"; } 

CONCLUSION:

 This is valid => matched [This is valid] This \"is Valid => matched [This \"is Valid] This is al\"so Valid\" => matched [This is al\"so Valid\"] This i"s invalid => didn't match This i"s inv"alid => didn't match This is a \"test => matched [This is a \"test] This is a \"test of " the emergency broadcast system - invalid => didn't match 
+1
source

Source: https://habr.com/ru/post/905219/


All Articles