grep with regex for phone number

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

提交回复
热议问题