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. ...
 
随机推荐
- Scala学习——Scala By Example——to be continued
			
这一篇是来自官网的Scala By Example 即Tutorial后更详细地对Scala怎么用给了示例 该文一开始给了一个快速排序的示例 版本一: def sort(xs: Array[Int]) ...
 - Spark系列-初体验(数据准备篇)
			
Spark系列-初体验(数据准备篇) Spark系列-核心概念 在Spark体验开始前需要准备环境和数据,环境的准备可以自己按照Spark官方文档安装.笔者选择使用CDH集群安装,可以参考笔者之前的文 ...
 - Linux配置CentOs7.4(网络连接处理)
			
说明:CentOS 7.0默认安装好之后是没有自动开启网络连接的! 进入登录界面 账号输入root 回车 再输入上面设置的root密码回车 系统登录成功 设置IP地址.网关DNS cd /etc/s ...
 - Day8 Servlet
			
HttpServletRequest 说明 公共接口类HttpServletRequest继承自ServletRequest.客户端浏览器发出的请求被封装成为一个HttpServletRequest对 ...
 - 看到了一个pipeline例子,
			
pipeline { agent any options { timestamps() } parameters { string(name: 'GIT_BRANCH', defaultValue: ...
 - Java实现Package编译和访问
			
Java实现Package编译和访问 说明 所有文件都是使用UTF-8编码来写的,请不要用Windows记事本随便打开 Test.java文件中注释的方法说明了该类是不能访问其方法的 文件目录树 bi ...
 - Win32 HTTP Download
			
头文件HTTPClient.h: #pragma once #ifndef HTTPClient_H_ #define HTTPClient_H_ #include <string> us ...
 - 关于ISP、IAP、DFU和bootloader
			
这是嵌入式开发中常用的几个专业术语,其诞生的背景和其具体作用大概如下 在很久很久以前,那是8051单片机流行的时代,做单片机开发都需要一个专用工具,就是单片机的编程器,或者叫烧写器.说“烧”写一点 ...
 - ra寄存器定位core
			
$ra寄存器中存入的是pc的值(程序运行处的地址),调用函数时,在跳转前,必须保存当前地址(pc的值),以便后来返回.jal $ra 保存后跳转,jr $ra,返回到跳转前,通过$ra保存进入上层栈地 ...
 - ios宏定义学习
			
宏简介: 宏是一种批量处理的称谓.一般说来,宏是一种规则或模式,或称语法替换 ,用于说明某一特定输入(通常是字符串)如何根据预定义的规则转换成对应的输出(通常也是字符串).这种替换在预编译时进行,称作 ...