Looking for Regex for phone no.

Discussion in 'Programming' started by protocol96, Nov 8, 2007.

  1. #1
    Hi,

    Can somebody please help me with regex for phone no.
    for below mentioned format only
    xxxxxxxxxx
    xxx.xxx.xxxx
    xxx-xxx-xxxx
     
    protocol96, Nov 8, 2007 IP
  2. nasty.web

    nasty.web Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    HTH

    \b\d{10}\b
    \b\d{3}\.\d{3}\.\d{4}\b
    \b\d{3}-\d{3}-\d{4}\b
     
    nasty.web, Nov 16, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    In one pattern:

    \d{3}[\.-]?\d{3}[\.-]?\d{4}
    or
    \d{3}([\.-])?\d{3}\\1\d{4}
     
    krt, Nov 16, 2007 IP
    nasty.web likes this.
  4. nasty.web

    nasty.web Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    to be precise:
    \b\d{3}[\.-]?\d{3}[\.-]?\d{4}\b
     
    nasty.web, Nov 16, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    I don't give that because protocol96 might want to do something else so it is up to him whether he wants to use start/end (^ and $) or word boundaries.

    Besides, I wouldn't use word boundaries here as this is most likely for a single field so you wouldn't want to match something like this:
    http://example.com/spamspamspam 123-456-7890
     
    krt, Nov 16, 2007 IP
  6. nasty.web

    nasty.web Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I agree with that. But without boundaries it can match something like this:

    123-123-45678
    8123-123-45678
    78123-1238456

    And so on.
    Also I've chosen 3 separate regexps to avoid situations like:

    123-123.1234
    123123-1234

    I think everything is up to protocol96 ; He has enough info here ;)
     
    nasty.web, Nov 16, 2007 IP
  7. freshwrite

    freshwrite Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    \d{3}([.-]?)\d{3}([.-]?)\d{4}

    This is what I would use in perl, i don't think it needs the backslash before the . inside the []
     
    freshwrite, Nov 19, 2007 IP