Reputation: 199
String s = "1.01";
Matcher matcher = Pattern.compile("[+-/\\*\\^\\%]").matcher(s);
if (matcher.find()) {
System.out.println(matcher.group());
}
Input string is "1.01" and output is ".". I can't understand why matcher.find() returns true, there are no symbols like "+", "-", "*", "^", "%" in input string. Why did it happen?
Upvotes: 7
Views: 393
Reputation: 4010
I'm pretty sure you have to escape -
. -
is used as a range symbol in character classes like in [0-9]
. The -
needs to be escaped if you want to find examples of the dash.
If you reorder the symbols inside, you can get away with the entire pattern without any escapes. [-+*^%]
should work and is a bit easier to read.
Upvotes: 6
Reputation: 37813
The dash in any other position than the first or last inside a character class denotes a character range, just like [a-z]
matches every lowercase letter from a to z, but [-az]
only matches the dash, the a and the z.
If you look at http://www.asciitable.com/, you'll see that [+-/]
will match any of +,-./
Also, you don't have to escape those symbols in a regex, especially not in a character class. As said before, your main problem is the position of the dash in the character class.
You can fix your regex from
"[+-/\\*\\^\\%]"
to
"[-+/\\*\\^\\%]"
^^
or without the unnecessary escaping:
"[-+/*^%]"
Upvotes: 6