Regex error for string
This is not a greedy form:
".*?"
*?
means: Match as little as possible, and *
means Match as much as possible.
The latter basically continues to the end of the string, returning characters one by one, so that the final one "
can match. This is why you get everything between the first and the very last quote on your line.
// for the sake of completeness: Java would need this pattern string
"\".*?\""
source to share
You are using a greedy quantifier. You want to use a reluctant quantifier instead.
Javadocs for Pattern should help: http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
On this page you will find the following:
Greedy quantifiers
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times
Reluctant quantifiers
X?? X, once or not at all
X*? X, zero or more times
X+? X, one or more times
X{n}? X, exactly n times
X{n,}? X, at least n times
X{n,m}? X, at least n but not more than m times
source to share
As other answers point out, the () quantifier is greedy and tries to match as many characters as possible. One way is "\" [^ \ "] \" ", so no" occurs in the middle. But you really want a reluctant quantifier that tries as many characters as possible. In your case, "\". *? \ "" The quantizer is reluctantly equal to * ?.
More on this here . "The differences between greedy, reluctant, and domineering quantifiers" may be particularly interesting here.
source to share