mint
mint

Reputation: 3433

RegEx Help (13 length digit, ends in specific digits)

Another RegEx question for a search I'm trying to do.

The Problem: Determine if user input is 13 digits long (numbers only) and ends with 52,56, or 57.

My current not working solution: [0-9]52|56|57${13}. I also tried [0-9]52|56|57{13}$ but no dice.

Help is appreciated.

Upvotes: 0

Views: 4497

Answers (2)

mrk
mrk

Reputation: 3191

You have 13 digits 11 of which can be any digit and the next two must be 52,56, or 57. 11 digits in regex notation is something like \d{11}. The 'or' operator is denoted by the symbole | so 52 or 56 or 57 becomes 52|56|57. The resulting regex looks like \d{11}(52|56|57). Parentheses are used to disambiguate.

Remember that you can construct complex regular expressions from simpler ones. This approach works always ;)

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838096

Try this regular expression:

^[0-9]{11}(52|56|57)$

This is what it means:

^          Start of string
[0-9]      Any character in 0-9
{11}       The previous token must match exactly 11 times.
(52|56|57) Alternation - any one of the possibilities must match
$          End of string

You could also in this case simplify it to:

^[0-9]{11}5[267]$

Upvotes: 5

Related Questions