grep with regex for phone number

后端 未结 11 2138
盖世英雄少女心
盖世英雄少女心 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 22:57

    We can put all the required phone number validations one by one using an or condition which is more likely to work well (but tiresome coding).

    grep '^[0-9]\{10\}$\|^[0-9]\{3\}[-][0-9]\{3\}[-][0-9]\{4\}$\|^[0-9]\{3\}[ ][0-9]\{3\}[ ][0-9]\{4\}$\|^[(][0-9]\{3\}[)][0-9]\{3\}[-][0-9]\{4\}$' phone_number.txt
    

    returns all the specific formats :

    • 920-702-9999
    • (920)702-9999
    • 920 702 9999
    • 9207029999
    0 讨论(0)
  • 2020-12-04 23:00

    There are usually four patterns of phone numbers

    1. xxx-xxx-xxxx         grep -o '[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}'  file.txt
    2. (xxx)xxx-xxxx        grep -o '([0-9]\{3\})[0-9]\{3\}\-[0-9]\{4\}'  file.txt
    3. xxx xxx xxxx         grep -o '[0-9]\{3\}\s[0-9]\{3\}\s[0-9]\{4\}'  file.txt
    4. xxxxxxxxxx           grep -o '[0-9]\{10\}' file.txt
    

    In all

    grep -o '\([0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}\)\|\(([0-9]\{3\})[0-9]\{3\}\-[0-9]\{4\}\)\|\([0-9]\{10\}\)\|\([0-9]\{3\}\s[0-9]\{3\}\s[0-9]\{4\}\)' file.txt
    

    Of course, one could simplify the regex above but we can also leave this simplification to grep itself ~

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

    My first thought is that you may find it easier to see if your candidate number matches against one of four regular expressions. That will be easier to develop/debug, especially as/when you have to handle additional formats in the future.

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

    Try this one:

    ^(\d{10}|((([0-9]{3})\s){2})[0-9]{4}|((([0-9]{3})\-){2})[0-9]{4}|([(][0-9]{3}[)])[0-9]{3}[-][0-9]{4})$

    This is only applicable for the formate you mention above like:

    1. xxxxxxxxxx
    2. xxx xxx xxxx
    3. xxx-xxx-xxxx
    4. (xxx)xxx-xxxx
    0 讨论(0)
  • 2020-12-04 23:06
    grep -P '[0-9]{3}-[0-9]{3}-[0-9]{3}|[0-9]{3}\ [0-9]{3}\ [0-9]{3}|[0-9]{9}|\([0-9]{3}\)[0-9]{3}-[0-9]{3}'
    
    0 讨论(0)
  • 2020-12-04 23:09
    grep '\(([0-9]\{3\})\|[0-9]\{3\}\)[ -]\?[0-9]\{3\}[ -]\?[0-9]\{4\}' file
    

    Explanation:

    ([0-9]\{3\}) three digits inside parentheses

    \| or

    [0-9]\{3\} three digits not inside parens

    ...with grouping parentheses - \(...\) - around the alternation so the rest of the regex behaves the same no matter which alternative matches.

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