Help with Regular Expression

Last post 10-27-2007 12:33 PM by jjepen. 2 replies.

Sort Posts:

  • Help with Regular Expression

    10-27-2007, 1:05 AM
    • Loading...
    • jjepen
    • Joined on 04-23-2005, 12:35 AM
    • Central Washington
    • Posts 61

    I need help creating a regular expression that validates a US telephone number with or without extension.  It needs to require an area code enclosed in parentheses and separated by a space, require a dash(-) between the prefix and the number, and allow an extension up to 4 digits(ex. x1234).  Some matches and non-matches are provided below.

    Match    (789) 555-1234
    Match   (849) 555-1234 x1234
    Match   (484) 555-8764 x1
    Non-Match   789-555-7894
    Non-Match   7895551234
    Non-Match    (546) 555-1354 x12345
    Non-Match   (345) 555-8794 x
    Non-Match 756 468 4657

    I've searched the net a found a few that claim to meet my requirements but under testing they all have some loophole that matches something it should not.  Any help would be greatly appreciated.
     

  • Re: Help with Regular Expression

    10-27-2007, 2:44 AM
    Answer

    Hello,

    Check these

     

    http://regexlib.com/DisplayPatterns.aspx
    http://www.regular-expressions.info/

     

    Mehedi Hasan


    Mark as answer if the post meets your requirement!
  • Re: Help with Regular Expression

    10-27-2007, 12:33 PM
    • Loading...
    • jjepen
    • Joined on 04-23-2005, 12:35 AM
    • Central Washington
    • Posts 61

    Thanks, I had been just looking for a regex I could copy because it looked a bit daunting but going through some of the tutorials it isn't all that bad.  This is what I came up with if anyone else needs something like it:

    \(\d{3}\)\s\d{3}-\d{4}(\s(x\d+)?){0,1}$ 

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter