En-Motion
En-Motion

Reputation: 919

Alphanumeric Regular Expression String

I am trying to write a regex for an alphanumeric string.

The following are all valid characters:

+0123456789BC

Valid strings are:

+445677BBCC
12345
44556677 etc

Basically rules are:

Currently have:

^\+?[0-9]+[BC]+?$

But this is not exactly what I need.

Any help?

Upvotes: 0

Views: 64

Answers (1)

Vivin Paliath
Vivin Paliath

Reputation: 95498

I think you will have to escape the +, and the metacharacter after [BC] needs to be * for "zero or more" instead of + for one or more. You also don't need the ? at the end since there's no need to make this a non-greedy match; you're matching till the end of the string anyway:

/^\+?\d+[BC]*$/

Otherwise, what you have is not really a valid regex. There is nothing to repeat for the + at the beginning, and the ^ is just an anchor for the beginning of the string.

Upvotes: 5

Related Questions