I'm trying to find all of the quoted text on a single line.
Example:
"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:
parsing ""[^"\]*(?:\.[^"\]*)*"" - Unterminated [] set.
How do I fix this?
What you've got there is an example of Friedl's "unrolled loop" technique, but you seem to have some confusion about how to express it as a string literal. Here's how it should look to the regex compiler:
"[^"\\]*(?:\\.[^"\\]*)*"
The initial "[^"\\]*
matches a quotation mark followed by zero or more of any characters other than quotation marks or backslashes. That part alone, along with the final "
, will match a simple quoted string with no embedded escape sequences, like "this"
or ""
.
If it does encounter a backslash, \\.
consumes the backslash and whatever follows it, and [^"\\]*
(again) consumes everything up to the next backslash or quotation mark. That part gets repeated as many times as necessary until an unescaped quotation mark turns up (or it reaches the end of the string and the match attempt fails).
Note that this will match "foo\"-
in \"foo\"-"bar"
. That may seem to expose a flaw in the regex, but it doesn't; it's the input that's invalid. The goal was to match quoted strings, optionally containing backslash-escaped quotes, embedded in other text--why would there be escaped quotes outside of quoted strings? If you really need to support that, you have a much more complex problem, requiring a very different approach.
As I said, the above is how the regex should look to the regex compiler. But you're writing it in the form of a string literal, and those tend to treat certain characters specially--i.e., backslashes and quotation marks. Fortunately, C#'s verbatim strings save you the hassle of having to double-escape backslashes; you just have to escape each quotation mark with another quotation mark:
Regex r = new Regex(@"""[^""\\]*(?:\\.[^""\\]*)*""");
So the rule is double quotation marks for the C# compiler and double backslashes for the regex compiler--nice and easy. This particular regex may look a little awkward, with the three quotation marks at either end, but consider the alternative:
Regex r = new Regex("\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"");
In Java, you always have to write them that way. :-(