Virginia
Virginia

Reputation: 41

Incorrect regex assignment

I'd like my regex to assign these and only these values: 1a, 1b, 2a, 2b, 3a, 3b, n. It seems that my regex is not working: "[([1-3][a-b])(n)]". Why it doesn't "see" the round bracket containing [1-3][a-b] and how should this regex look to work correctly?

Upvotes: 1

Views: 49

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

I'd like my regex to assign these and only these values: 1a, 1b, 2a, 2b, 3a, 3b, n.

That means, you need to use

.matches("[1-3][ab]|n")

See the regex demo

In your pattern, the outer square brackets created a character class and the inner ones just were treated as unions and the whole "[([1-3][a-b])(n)]" matched just 1 char (a (, or 1 to 3 digit, or a to b letter, etc.

Upvotes: 1

Related Questions