views:

3743

answers:

5

How to get substring " It's big \"problem " using a regexp

s = ' function(){ return " Is big \"problem "; }';

A: 
/(["\']).*?(?<!\\)(\\\\)*\1/is

should work with any quoted string

Nice, but too flexible for the request (will match single quotes...). And can be simplified to /".*?(?<!\\)"/ unless I miss something. Oh, and some languages (eg. JavaScript) alas doesn't understand negative lookbehind expressions.
PhiLho
@PhiLho, just using a single (?<!\\) would fail on escaped backslashes at the end of the string. True about look-behinds in JavaScript though.
MizardX
+3  A: 

Friedl's classic "unrolled-loop" pattern:

/"[^"\\]*(?:\\.[^"\\]*)*/
Alan Moore
Err, which regular expression syntax is this? In particular the ?:\\ thingy
ePharaoh
Managed to find an explanation here: http://ad.hominem.org/log/2005/05/quoted_strings.phpThe syntax there is more palatable.Anyway, thanks Alan for the pointer.
ePharaoh
A: 

One has to remember that regexps aren't a silver bullet for everything string-y. Some stuff are simpler to do with a cursor and linear, manual, seeking. A CFL would do the trick pretty trivially, but there aren't many CFL implementations (afaik).

Henrik Paul
True enough, but this problem is well within the capabilities of regexes, and there are a great many implementations of those.
Alan Moore
+5  A: 
/"(?:[^"\\]|\\.)*"/

Works in The Regex Coach and PCRE Workbench.

Example of test in JavaScript:

var s = ' function(){ return " Is big \\"problem\\", \\no? "; }';
var m = s.match(/"(?:[^"\\]|\\.)*"/);
if (m != null)
    alert(m);
PhiLho
A: 

This one comes from nanorc.sample available in many linux distros. It is used for syntax highlighting of C style strings

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