grep with regex for phone number

后端 未结 11 2139
盖世英雄少女心
盖世英雄少女心 2020-12-04 22:39

I would like to get the phone numbers from a file. I know the numbers have different forms, I can handle for a single one, but don\'t know how to get a uniform regex. For ex

相关标签:
11条回答
  • 2020-12-04 23:09

    I got this:

    debian:tmp$ cat p.txt
    333-444-5555
    (333)333-6666
    123 456 7890
    1234567890
    debian:tmp$ egrep '\(?[0-9]{3}[ )-]?[0-9]{3}[ -]?[0-9]{4}' p.txt
    333-444-5555
    (333)333-6666
    123 456 7890
    1234567890
    debian:tmp$ egrep --version
    GNU grep 2.5.3
    
    Copyright (C) 1988, 1992-2002, 2004, 2005  Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    debian:tmp$
    
    0 讨论(0)
  • 2020-12-04 23:16

    This is just a modified version of Alan Moore's solution. This is protected against some race condition where the last part of the number has more than four digits in it or the if the total number of digits are more than 10:

    grep '\(\(([0-9]\{3\})\|[0-9]\{3\}\)[ -]\?\)\{2\}[0-9]\{4\} '
    

    Explanation:

    1. \(([0-9]\{3\})\|[0-9]\{3\}\) matches exactly three digits (e.g. 234) with or without surrounded by parentheses. \| performs the 'OR' operation.
    2. The first \( ... \) groups together the above format followed by a space or - or no space at all - ([ -]\?) does that.
    3. The \{2\} matches exactly two occurrences of the above
    4. The [0-9]\{4\} ' matches exactly one occurrence for a 4 digit number followed by a space

    And it's a bit shorter as well. Tested on RHEL and Ubuntu. Cheers!!

    0 讨论(0)
  • 2020-12-04 23:20

    +?(1[ -])?((\d{3})[ -]|(\d{3}[ -]?)){2}\d{4}

    works for:

    123-678-1234

    123 678 1234

    (123)-678-1234

    +1-(123)-678-1234

    1-(123)-678-1234

    1 123 678 1234

    1 (123) 678 1234

    0 讨论(0)
  • 2020-12-04 23:21
    grep -oE '\(?\<[0-9]{3}[-) ]?[0-9]{3}[ -]?[0-9]{4}\>'
    

    Matches all your formats.

    The \< and \> word boundaries prevent matching numbers that are too long, such as 123-123-12345 or 1234-123-1234

    0 讨论(0)
  • 2020-12-04 23:22

    You can just OR (|) your regexes together -- will be more readable that way too!

    0 讨论(0)
提交回复
热议问题