# RegEx-golf: match all contents in a string

Your task is to write a RegEx that matches everything inside strings.

A string is defined as everything surrounded by (but not including) two unescaped ".

A " can be escaped by \, which can also be escaped again.

# Testcases

string:  ab\c"defg\\\"hi"jkl"mn\\\\"opqrst""
matches:      ^^^^^^^^^^     ^^^^^^        ^ (the empty string)


# Scoring

Shortest solution wins.

# Specs

• Please specify the flavour used.
• The input will have balanced ".
• There will be no \ that immediately precedes a string-beginning-delimiter. For example, you would not need to handle abc\"def"
• Will there be \ before a string? For example abc\"def". – jimmy23013 May 20 '16 at 10:29
• Should it match each string in one group? For example, could I write something that has two matches in abc"de", one is d and the other is e? – jimmy23013 May 20 '16 at 10:43
• It is allowed . – Leaky Nun May 20 '16 at 10:44
• Will there be empty strings? – Martin Ender May 20 '16 at 10:45
• Yes, there will be empty strings. – Leaky Nun May 20 '16 at 10:48

# PCRE, 212015 19 bytes

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


Try it here.

This matches a character (or the beginning of the input) before the beginning double quote and then reset the match, to make sure the double quote isn't shared with another match.

# PCRE, 25 23 bytes

Thanks to Martin Büttner for golfing off 2 bytes.

(\\.|[^"])*+(?!"(?R)|$)  Try it here. ### Explanation ( \\.|[^"] # An escaped character, or a character that isn't a double quote )*+ # Possessive zero-or-more quantifier, which means backtracking # could not happen after first match is found. That means if \\. # matched, it would never switch to [^"], because it is always a # match if it just stopped after the \\. without backtracking. (?!"(?R)|$)      # Make sure it is not followed by a double quote and another
# match, or the end of the input.


Note that the possessive quantifier (*+) made sure the negative lookahead always begins after a whole string, or a whole segment of non-string.

There are 4 cases:

• The match begins anywhere outside of a string. \\. would never match a double quote according to the clarification. It could only end just before the next double quote which begins a string, or the end of input. Both cases fails the negative lookahead.
• The match begins at the beginning of a string. (\\.|[^"])*+ would match a complete string. The next character must be a double quote, and couldn't be the end of input. After the double quote it is outside of the string, so it couldn't be another match. So it passes the negative lookahead.
• The match begins at the end of a string. It matches an empty string in the same way as the previous case. But it doesn't matter according to the clarification.
• The match begins in the middle of a string. Impossible because matches don't overlap.
• Would (\\.|[^"]) work? – Martin Ender May 20 '16 at 10:50
• @MartinBüttner that matches everything except " – Bálint May 20 '16 at 10:52
• @Bálint I meant in place of ([^\\"]|\\.), not as the complete solution. – Martin Ender May 20 '16 at 10:52
• @MartinBüttner Oh, ok – Bálint May 20 '16 at 10:56
• Martin's suggestion should work, since \\. only fails when there is no character after \ (or new line character, but that can be fixed with flag), and that case is covered by the negative look-behind. The possessive quantifier prevents backtracking, so we have no other case to look at. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ May 20 '16 at 11:03

# JavaScript, 24 bytes

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

Group 1 is the contents of the string.

• This doesn't at all work with escaped quotes, and thus fails to meet the spec. – ATaco Feb 15 '17 at 21:12
• Ah yes--sorry. How about that? – Whothehellisthat Feb 15 '17 at 22:20
• Close but no cigar, you shouldn't be matching the outer "s – ATaco Feb 15 '17 at 22:22
• Yeah, that's what I was afraid of. No way of doing it in JavaScript, I'm guessing? – Whothehellisthat Feb 15 '17 at 22:52
• You can capture it in a subgroup – ATaco Feb 15 '17 at 22:53

# JavaScript, 211513 12 bytes

"((\\?.)*?)"

String contents are in group 1.

"   #start of string
(    #capturing group
(
\\?. #match character or escaped character
)*?  #match as few as possible
)
"   #end of string