Reputation: 373
I am reviewing the c# code of an application and documenting. While going through the code, I seen an unusual regular expression for US Phone Number. The regular expression is below
@"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$";
My conclusion from the above phone number would be like 01.(29).123.4567 or 01-38-111-1111.
Am I interpreting correctly? Any insight into that regular expression is highly appreciated. Examples for above regular expression would help me to learn more about regular expressions.
Upvotes: 4
Views: 3182
Reputation: 25
[RegularExpression(@"^\d{2}-\d{10,15}$", ErrorMessage = "Invalid Phone no")]
it will take the number in the form of 91-xxxxxxxxxx
x is 10 to 15 numbers ranging
Upvotes: 0
Reputation: 25
^\d{2}-\d{3,15}$
It will help to ranging the mobile number from 3 t0 15 digits.
ex: 91-xxxxxxxxxxxxxxx
the above x
is ranging from 3 to 15 digits
[RegularExpression(@"^\d{2}-\d{3,15}$
", ErrorMessage = "Invalid Phone no")]
Upvotes: 0
Reputation: 3490
^[01]? = 0 or 1 or nothing
[-.]? = - or . or nothing
(([2-9]\d{2}) = first number 2 or greater followed by 2 digits which can be any number b/w 0 to 9 .. means 012 or 112 both will work fine
| = or sign
[2-9]\d{2}) = first number b/w 2 and 9 followed by 2 numbers b/w 0-9
[-.]? = means - or . or nothing
\d{4}= any four numbers b/w 0-9
$ = It make sure regex ended with what is preceding $ sign. ^ does same thing as $ but it checks only the beginning of the regex.
Upvotes: 0
Reputation: 490607
Reading left to right...
^[01]?
May possibly start with 0
or 1
.[- .]?
May possibly be followed with a -
, space or .
.(([2-9]\d{2})|[2-9]\d{2})
Must start with a digit between 2
and 9
and then be followed by any two digits. (This is strangely repeated twice, and the capturing groups should always contain the same portion, weird). This may have meant to have escaped the parenthesis, which would make more sense. Generally, you use the \
character to escape.[- .]?
May possibly be followed with a -
, space or .
.\d{3}
Must be followed by any three digits.[- .]?
May possibly be followed with a -
, space or .
.\d{4}$
Must be followed (and end) with any four digits.Upvotes: 11