LeetCode 193. Valid Phone Numbers
分析
难度 易
来源
https://leetcode.com/problems/valid-phone-numbers/
题目
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
解答
https://leetcode.com/problems/valid-phone-numbers/discuss/55478/Grep-e-solution-with-detailed-explanation-good-for-those-new-to-regex
grep -e '\(^[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}$\)' -e '\(^([0-9]\{3\})[ ]\{1\}[0-9]\{3\}-\([0-9]\{4\}\)$\)' file.txt
- In Bash, we use \ to escape next one trailing character;
- ^ is used to denote the beginning of a line
- $ is used to denote the end of a line
- {M} is used to denote to match exactly M times of the previous occurence/regex
- (...) is used to group pattern/regex together
Back to this problem: it requires us to match two patterns, for better readability, I used -e and separate the two patterns into two regexes, the first one matches this case: xxx-xxx-xxxx and the second one matches this case: (xxx) xxx-xxxx
加上-P
(使用Perl的正则引擎)即可过滤出目标数据
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
注意上方的空格
grep '^(\d{3}-|\(\d{3}\)[ ]{1})\d{3}-\d{4}$' file.txt
这里使用[ ]{1}表示一个空格
LeetCode 193. Valid Phone Numbers的更多相关文章
- 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 ...
- 193 Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] 193. Valid Phone Numbers_Easy tag: Bash
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [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 ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
随机推荐
- 给 Virtualbox 中 Ubuntu 系统设置静态 IP
虚拟机网络选择 桥接网卡 模式. 主要涉及两个步骤: 1. 修改 /etc/network/interfaces 文件: 2. 修改 dns : 第一步,修改 interfaces 文件: sudo ...
- IOS的滑动菜单(Sliding Menu)的具体写法(附代码)
滑动菜单是一个很流行的IOS控件 先上效果图: 这里使用github的JTReveal框架来开发,链接是https://github.com/agassiyzh/JTRevealSide ...
- prometheus-入门尝试
prometheus-入门 Prometheus 是由 SoundCloud 开源监控告警解决方案2015 年在 github 上开源以来,已经吸引了 很多大公司的使用:2016 年 Promethe ...
- 【node.js】全局变量、常用工具、文件系统
学习链接:http://www.runoob.com/nodejs/nodejs-global-object.html 在 JavaScript 中,通常 window 是全局对象, 而 Node.j ...
- 预备作业二——有关CCCCC语言(・᷄ᵌ・᷅)
有关CCCCC语言(・᷄ᵌ・᷅) 下面又到了回答老师问题的时候啦-(・᷄ᵌ・᷅) 有些问题正在深思熟虑中!敬请期待近期的不间断更新! 你有什么技能比大多人(超过90%以上)更好? 针对这个技能的获取你 ...
- 20145203盖泽双 《Java程序设计》第五周学习总结
20145203盖泽双 <Java程序设计>第五周学习总结 教材学习内容总结 1.Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生时显示友好的错误信息,还可以在捕 ...
- cpu负载过高分析
如何定位是哪个服务进程导致CPU过载,哪个线程导致CPU过载,哪段代码导致CPU过载? 步骤一.找到最耗CPU的进程 工具:top 方法: 执行top -c ,显示进程运行信息列表 键入P (大写p) ...
- Kafka设计解析(十五)Kafka controller重设计
转载自 huxihx,原文链接 Kafka controller重设计 目录 一.Controller是做什么的 二.Controller当前设计 三.Controller组成 四.Controlle ...
- HDU 1203 01背包变形题,(新思路)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1203 I NEED A OFFER! Time Limit: 2000/1000 MS (Java/ ...
- Servlet基础笔记
一.什么Servlet? servlet 是运行在 Web 服务器中的小型 Java 程序(即:服务器端的小应用程序).servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户 ...