<\/script>')

Regex for quoted string

How to get the substring " It big \"problem " using regular expression?

 s = ' function(){ return " It\ big \"problem "; }'; 
+78
regex escaping quotes
Oct 30 '08 at 10:53
source share
13 answers
 /"(?:[^"\\]|\\.)*"/ 

Works at Regex Coach and PCRE Workbench.

JavaScript test example:

  var s = ' function(){ return " Is big \\"problem\\", \\no? "; }'; var m = s.match(/"(?:[^"\\]|\\.)*"/); if (m != null) alert(m); 
+99
Oct 30 '08 at 11:59
source share

This comes from nanorc.sample, available on many linux distributions. It is used to highlight the syntax of C-style strings.

 \"(\\.|[^\"])*\" 
+22
Jun 19 '09 at 4:34
source share

As stated in ePharaoh, the answer is

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

To apply the above to single quotes or double quotes, use

 /"([^"\\]*(\\.[^"\\]*)*)"|\'([^\'\\]*(\\.[^\'\\]*)*)\'/ 
+12
May 28 '12 at 14:12
source share
 "(?:\\"|.)*?" 

Alternating \" and . Goes through escaped quotes, while the lazy *? Quantifier ensures you don't go past the end of the quoted string. Works with .NET Framework RE classes

+7
Dec 15 '10 at 8:54
source share

Most proposed solutions use alternative repetition paths, i.e. (A | B) *.

You may encounter stack overflow on large inputs, as some template compiler implements this using recursion.

Java, for example: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993

Something like this: "(?:[^"\\]*(?:\\.)?)*" , Or the one provided by Guy Bedford will reduce the number of parsing steps, avoiding most stack overflows.

+7
Jun 09 '15 at 16:02
source share
 /"(?:[^"\\]++|\\.)*+"/ 

Taken directly from man perlre on a Linux system with Perl 5.22.0 installed. As an optimization, this regular expression uses the "posessive" form of both + and * to prevent backtracking, since it is known in advance that a line without a closing quote will in no way match.

+5
Nov 09 '15 at 20:38
source share
 /(["\']).*?(?<!\\)(\\\\)*\1/is 

should work with any quoted string

+2
Oct 30 '08 at 10:58
source share

This works fine on PCRE and does not crash with StackOverflow.

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

Explanation:

  • Each quoted string begins with Char: " ;
  • It can contain any number of characters:. .*? {Lazy match}; ending with non escape [^\\] ;
  • The operator (2) is Lazy (!) Optional, because the string may be empty (""). So: (.*?[^\\])??
  • Finally, each quoted string ends with Char ( " ), but it may be preceded by an even number of escape code pairs (\\\\)+ ; and this Greedy (!) Is optional: ((\\\\)+)?+ {Greedy matching}, the bacause string may be empty or without trailing pairs!
+2
Apr 24 '17 at 20:17
source share

here is what works with both “and” and easily adds others at the beginning.

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

it uses a backlink (\ 1) to exactly match what is in the first group ("or").

http://www.regular-expressions.info/backref.html

+1
Aug 05 '17 at 22:37
source share

Keep in mind that regular expressions are not a silver bullet for all -y strings. Some things are easier to do with the cursor and linear, manual search. A CFL could do the trick quite trivially, but there are not many CFL implementations (afaik).

0
Oct. 30 '08 at 11:18
source share

If the search is done from the very beginning, maybe this can work?

 \"((\\\")|[^\\])*\" 
0
Apr 10 '13 at 21:14
source share

A more extensive version of https://stackoverflow.com/a/312618/

 /"([^"\\]{50,}(\\.[^"\\]*)*)"|\'[^\'\\]{50,}(\\.[^\'\\]*)*\'|"[^"\\]{50,}(\\.[^"\\]*)*"/ 

This version also contains

  • Minimum Quote Length 50
  • Additional types of quotes (open " and close " )
0
Dec 03 '13 at
source share

Noted in regexpal and ended up with this regex: (Don't ask me how this works, I barely understand even tho I wrote this lol)

 "(([^"\\]?(\\\\)?)|(\\")+)+" 
0
Sep 20 '14 at 22:54
source share



All Articles