SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict The Mog's Avatar
    Join Date
    Dec 2002
    Location
    Manchester UK
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question regexPattern help needed

    Hi guys,

    can someone please help me write a regexPattern for a telephone number

    it can be either of the formats below:

    +44 545 202 6545
    +44 (0) 545 202 6546
    0545 202 6540
    05452026501

    so either with / without the leading +
    might have (0)
    might have spaces

    Please guys any help will be great

    Thanks

    K-

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Thanks for clearly explaining what you require.

    I'll use grouping parentheses to keep thing together for these examples.

    The +44 is optional so that is (\+\d{2})?
    Optional spaces are \s?
    The (0) is optional which is (\(\d\))? but the brackets are optional too, which is instead (\(?\d\)?)? and there is another optional space \s?
    Then you have a group of three numbers with an optional space (\d{3})\s?
    And another group of three numbers with an optional space (\d{3})\s?
    And another four numbers (\d{4})

    Put that all together and you have
    (\+\d{2})?\s?(\(?\d\)?)?\s?(\d{3})\s?(\d{3})\s?(\d{4})

    You can test it out at http://www.regular-expressions.info/...ptexample.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •