Regex error for string

I have a simple regex

"\".*\""

      

for me he says that he chooses everything between "and", but he also catches

"text") != -1 || file.indexOf(".exe"

      

for me two lines of it, for regular expressions - one. how can I get the regex to see that its two lines?

PS I am using Java.

+1


source to share


8 answers


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
"\".*?\""

      

+12


source


Regular expressions are greedy. What you want to do is exclude quotes from the middle of the match, like



"\"[^\"]*\""

      

+6


source


.

Use instead [^\"]

so the regex can't match"

+2


source


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

      

+2


source


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.

+1


source


0


source


Find a way to specify non-greedy behavior in Java regexp.

0


source


Do you know how many characters there will be, or at least max? If so, you can use \ ". {N,} \" where n is the maximum or omit "," if you know the exact length.

0


source







All Articles