xxx222
xxx222

Reputation: 3244

Where am I doing wrong with regular expression in Java?

I want my operator variable matches a list of possible operators ("+", "-", "*", "/", "^") for exactly once, so I did

operator.matches(Pattern.quote("[+-*/^]??"))

It doesn't work. Did I do anything wrong?

Upvotes: 2

Views: 50

Answers (1)

SOFe
SOFe

Reputation: 8214

Pattern.quote would quote your whole input. It will not magically ignore your []?? and only look at things inside the [].

You should use this instead.

operator.matches("[" + Pattern.quote("+-*/^") + "]??")

Upvotes: 4

Related Questions