Question: I'm trying to find all of the quoted text on a single line.

Example:

```none
"Some Text"
"Some more Text"
"Even more text about \"this text\""

```

I need to get: 

* `"Some Text"`
* `"Some more Text"`
* `"Even more text about \"this text\""`

`\"[^\"\r]*\"` gives me everything except for the last one, because of the escaped quotes.

I have read about `\"[^\"\\]*(?:\\.[^\"\\]*)*\"` working, but I get an error at run time:

```none
parsing ""[^"\]*(?:\.[^"\]*)*"" - Unterminated [] set.

```

How do I fix this?

Answer: 
Regex for capturing strings (with `\` for character escaping), for the .NET engine:

```
(?>(?(STR)(?(ESC).(?<-ESC>)|\\(?<ESC>))|(?!))|(?(STR)"(?<-STR>)|"(?<STR>))|(?(STR).|(?!)))+   

```

Here, a "friendly" version:

```
(?>                            | especify nonbacktracking
   (?(STR)                     | if (STRING MODE) then
         (?(ESC)               |     if (ESCAPE MODE) then
               .(?<-ESC>)      |          match any char and exits escape mode (pop ESC)
               |               |     else
               \\(?<ESC>)      |          match '\' and enters escape mode (push ESC)
         )                     |     endif
         |                     | else
         (?!)                  |     do nothing (NOP)
   )                           | endif
   |                           | -- OR
   (?(STR)                     | if (STRING MODE) then
         "(?<-STR>)            |     match '"' and exits string mode (pop STR)
         |                     | else
         "(?<STR>)             |     match '"' and enters string mode (push STR)
   )                           | endif
   |                           | -- OR
   (?(STR)                     | if (STRING MODE) then
         .                     |     matches any character
         |                     | else
         (?!)                  |     do nothing (NOP)  
   )                           | endif
)+                             | REPEATS FOR EVERY CHARACTER

```

Based on <http://tomkaminski.com/conditional-constructs-net-regular-expressions> examples. It relies in quotes balancing. I use it with great success. Use it with `Singleline` flag.

To play around with regexes, I recommend [Rad Software Regular Expression Designer](http://www.radsoftware.com.au/regexdesigner/), which has a nice "Language Elements" tab with quick access to some basic instructions. It's based at .NET's regex engine.
```
"(\\"|\\\\|[^"\\])*"

```

should work. Match either an escaped quote, an escaped backslash, or any other character except a quote or backslash character. Repeat. 

In C#:

```
StringCollection resultList = new StringCollection();
Regex regexObj = new Regex(@"""(\\""|\\\\|[^""\\])*""");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Value);
    matchResult = matchResult.NextMatch();
} 

```

Edit: Added escaped backslash to the list to correctly handle `"This is a test\\"`.

Explanation:

First match a quote character.

Then the alternatives are evaluated from left to right. The engine first tries to match an escaped quote. If that doesn't match, it tries an escaped backslash. That way, it can distinguish between `"Hello \" string continues"` and `"String ends here \\"`.

If either don't match, then anything else is allowed except for a quote or backslash character. Then repeat.

Finally, match the closing quote.