Pattern.matches("[A(BC)]", "BC") why this returns false?
Asked
Active
Viewed 66 times
-4
-
1I'm pretty sure that regex doesn't do what you think it does. You may want to read [learning regular expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions). – Aran-Fey Jun 11 '17 at 10:45
1 Answers
2
Because the pattern expects to see a single character from the class A(BC)
, and matches
matches the entire input against the regex (doesn't look for partial matches). Since the input is two characters, it isn't a match.

T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875
-
-
1@lukhol: No. `(` and `)` have no special meaning within a character class (`[...]`). *Outside* of one, they form a capture group, but it's a capture group that would capture `BC`, not *either* `B` or `C`. I suggest stepping back from your current task and working through some basic Java regular expression tutorials. – T.J. Crowder Jun 11 '17 at 10:48