grep with regex for phone number

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

提交回复
热议问题