Regular Expressions, Checking for a range of occurrences

Posted by gmcalab on Stack Overflow See other posts from Stack Overflow or by gmcalab
Published on 2010-03-30T14:10:32Z Indexed on 2010/03/30 14:13 UTC
Read the original article Hit count: 358

Filed under:

I have a phone number I want to match against a regular expression. The format of the phone number must match this:

(123) 123-4567 x12345

The extension is optional. Also the extension must contain 1-5 numbers. Below is a regular expression I wrote that works.

^\(\d{3}\) \d{3}-\d{4}( x\d\d?\d?\d?\d?)?$

I was wondering if there is a better way to check for the extension instead of

x\d\d?\d?\d?\d?

Can I say 1-5 occurrences of \d instead of the above some how ?

© Stack Overflow or respective owner

Related posts about regular-expression