-4

Pattern.matches("[A(BC)]", "BC") why this returns false?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
lukhol
  • 318
  • 2
  • 6
  • 18
  • 1
    I'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 Answers1

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
  • Circe bracket doesn't group BC to the 'single character'? – lukhol Jun 11 '17 at 10:45
  • 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