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.

For 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

这道题让我们验证数字串是否为正确的电话号码的格式,而且规定了正确的格式只有两种(xxx) xxx-xxxx or xxx-xxx-xxxx,那么我们可以看出来区别就是在前几个字符,而后八个字符都相同。这题有多种解法,我们首先来看使用awk命令的解法,关于awk的介绍可以参见这个帖子。这道题是难点是如何写匹配的正则表达式,关于Bash脚本的正则表达式讲解请参见这个贴子。那么首先来看‘/.../'表示中间的是要匹配的正则表达式,然后脱字符^匹配一行的开头,美元符$在正则表达式中匹配行尾,然后再看中间的部分,[0-9]{3}表示匹配三个数字,圆括号括起一组正则表达式. 它和"|"操作符或在用expr进行子字符串提取(substring extraction)一起使用很有用。那么([0-9]{3}-|\([0-9]{3}\) )就可以理解了,它匹配了xxx-和(xxx) 这两种形式的字符串,然后后面的就好理解了,匹配xxx-xxxx这样的字符串,参见代码如下:

解法一:

awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt

下面来看使用sed命令的解法,关于sed的讲解可以参见这个帖子。那么我们先来看后面的两个参数,-n表示关闭默认输出,默认将自动打印所有行,这样就不会打印出不符合要求的数字串了。-r表示支持扩展正则+ ? () {} |。后面的正则表达式和上面都相同,就是后面多了一个p,在用sed时,p和-n合用,表示打印某一行,这样才能把符合要求的行打印出来:

解法二:

sed -n -r '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt

再来看使用grep命令的做法,关于grep的讲解可以参见这个帖子。我没有查到那个-P参数的用法,有没有大神来点拨一下,后面的正则表达式思路根上面的相同,只不过用d{3}来表示[0-9]{3},道理都一样,参见代码如下:

解法三:

 grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt

参考资料:

https://leetcode.com/discuss/29282/this-is-my-simple-solution

https://leetcode.com/discuss/29476/three-different-solutions-using-grep-sed-and-awk

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Valid Phone Numbers 验证电话号码的更多相关文章

  1. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  2. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  3. [LeetCode] Valid Palindrome II 验证回文字符串之二

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  4. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  5. [LeetCode] Valid Tic-Tac-Toe State 验证井字棋状态

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  6. [Bash]LeetCode193. 有效电话号码 | Valid Phone Numbers

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  7. LeetCode(193. Valid Phone Numbers)(sed用法)

    193. Valid Phone Numbers Given a text file file.txt that contains list of phone numbers (one per lin ...

  8. LeetCode 193. Valid Phone Numbers

    分析 难度 易 来源 https://leetcode.com/problems/valid-phone-numbers/ 题目 Given a text file file.txt that con ...

  9. C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编

      验证电话号码的主要代码如下: public bool IsTelephone(string str_telephone) { return System.Text.RegularExpressio ...

随机推荐

  1. Chrome调试中的奇技淫巧

    网上有关Chrome调试的文章一搜一大堆,本文主要记录一下自己平时经常用并且又比较冷门的一些技巧. 打开Chrome调试工具 1.打开控制台的情况下,长按页面的“刷新”按钮可以选择按何种方式刷新(有正 ...

  2. js实现StringBuffer

    实现 function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.Append = function (str ...

  3. CE修改器修改DNF 测试视频 阿修罗提升智力增加攻击力

    使用CE修改器来修改网络游戏,如DNF 测试视频: CE修改器:指的是Cheat Engine,字面上的意思指的是作弊引擎的意思,是一款内存修改编辑工具.通过修改游戏的内存数据来得到一些原本无法实现的 ...

  4. Node基础篇(文件操作)

    文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...

  5. MacOS平台下@rpath在动态链接库中的应用

    一.背景介绍 公司开发的一个底层库被用在了Mac平台的多个产品中.在开发这个底层库的初期,对于Mac OSX下的Install name 并没有过多的了解.对于XCode中的install name项 ...

  6. C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试

    在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...

  7. Spring下ActiveMQ实战

    MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...

  8. 口碑外卖系统架构图(li)

  9. [连载]《C#通讯(串口和网络)框架的设计与实现》- 8.总体控制器的设计

    目       录 第八章           总体控制器的设计... 2 8.1           总控制器的职能... 2 8.2           组装和释放部件... 3 8.3      ...

  10. Hibernate(二)__简单实例入门

    首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...