193. Valid Phone Numbers

Problem

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

Example

Assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890

Your script should output the following valid phone numbers:

987-123-4567
(123) 456-7890

Code

cat file.txt | grep -P '(^(\d{3}-){2}\d{4}$)|(^\(\d{3}\)[ ]\d{3}-\d{4}$)' -o

Result

193. Valid Phone Numbers.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,032评论 0 10
  • 193. Valid Phone Numbers Given a text file file.txt that ...
    rochestor阅读 441评论 0 0
  • 广播里播放着“生活不知眼前的苟且,还有诗和远方的田野”抬头仰望星空,很明亮,有着大城市看不到的星星。你很美却离我很...
    半亩荒阅读 333评论 0 1
  • 1.传统制造行业的应收账款 一、基本情况 传统制造行业接受下游客户订单进行生产制造、交货所产生的应收账款,此类型的...
    晚清有个李鸿章阅读 745评论 1 2
  • 光阴是除夕的饺子 咬一口 送走一年 昔日父母喊我 热腾腾的菜饭 今天我敬父母一缕青烟 爱人给我剪指甲 剪了长 长了...
    S丫J阅读 326评论 7 14

友情链接更多精彩内容