grep with regex for phone number

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

    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.

提交回复
热议问题