Regex blues

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

I am trying to come up with a regex to validate a US phone number with
an optional extention.

These are valid

714-895-8832
714-895-8832 x12345
(714) 895-8832
(714) 895-8832 x12345

Does anyone have one handy?

Thanks.
 
Frank said:
I am trying to come up with a regex to validate a US phone number with
an optional extention.

These are valid

714-895-8832
714-895-8832 x12345
(714) 895-8832
(714) 895-8832 x12345

Regex regex = new Regex(@"(\(?\d{3}\)?(?:\s?|-)\d{3}-\d{4}\s?(?:x\d{5})?)",
(RegexOptions) 0);

Note that the space before the extension is optional so that the regex will also capture
(714) 895-8832x12345.
 
You are the man.

Ken said:
Regex regex = new
Regex(@"(\(?\d{3}\)?(?:\s?|-)\d{3}-\d{4}\s?(?:x\d{5})?)",
(RegexOptions) 0);

Note that the space before the extension is optional so that the regex
will also capture
(714) 895-8832x12345.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top