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. ...
随机推荐
- 针对 Linux 环境下 gdb 动态调试获取的局部变量地址与直接运行程序时不一致问题的解决方案
基础的缓冲区溢出实践通常需要确定运行状态下程序中的某些局部变量的地址,如需要确定输入缓冲区的起始地址从而获得注入缓冲区中的机器指令的起始地址等.在 Linux 环境下,可通过 gdb 对程序进行动态调 ...
- Zookeeper ZAB 协议分析[转]
写在开始:这是我找到一篇比较好的博客,转载到这来进行备份原文参考: Zookeeper ZAB 协议分析 前言 ZAB 协议是为分布式协调服务 ZooKeeper 专门设计的一种支持崩溃恢复的原子广播 ...
- virtualbox+vagrant学习-2(command cli)-22-vagrant validate命令
Validate 格式: vagrant validate [options] 该命令用于验证你的Vagrantfile文件 userdeMacBook-Pro:~ user$ vagrant val ...
- python110道面试题
1.一行代码实现1--100之和 利用sum()函数求和 2.如何在一个函数内部修改全局变量 利用global 修改全局变量 3.列出5个python标准库 os:提供了不少与操作系统相关联的函数 s ...
- JDK(九)JDK1.7源码分析【集合】HashMap的死循环
前言 在JDK1.7&1.8源码对比分析[集合]HashMap中我们遗留了一个问题:为什么HashMap在调用resize() 方法时会出现死循环?这篇文章就通过JDK1.7的源码来分析并解释 ...
- P1736 创意吃鱼法
题目描述 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢(猫猫就是这么可爱,吃鱼也要想好吃法 ^_*).她发现,把大池子视为01矩阵(0表示对应位置无鱼,1 ...
- kubernetes 日志定制查阅 - 排错 -- 好用的命令
You can use kubectl logs to retrieve logs from a previous instantiation of a container with --previo ...
- oracle 事务 数据伪列
在用户进行数据更新操作(DML)事务一定会起作用. 事务的出现会保证数据的完整性.一致性.在整个事务的处理过程之中主要使用两个操作命令: · 事务的提交(COMMIT):是真正的向数据库之中发出更新指 ...
- oracle快速学习
- 【PHP开发规范】老生常谈的编码开发规范你懂多少?
[PHP开发规范]老生常谈的编码开发规范你懂多少? 这几天看了一下阿里技术发布的一套Java开发规范<阿里巴巴Java开发手册>,里面写了阿里内部的Java开发规范标准,写的很好.这套Ja ...