分析

难度 易

来源

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
  1. In Bash, we use \ to escape next one trailing character;
  2. ^ is used to denote the beginning of a line
  3. $ is used to denote the end of a line
  4. {M} is used to denote to match exactly M times of the previous occurence/regex
  5. (...) 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的更多相关文章

  1. 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 ...

  2. 193 Valid Phone Numbers

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

  3. [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 ...

  4. [LeetCode] Valid Phone Numbers 验证电话号码

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

  5. [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 ...

  6. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  7. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  8. Valid Phone Numbers

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

  9. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

随机推荐

  1. HBase学习之路 (十)HBase表的设计原则

    建表高级属性 下面几个 shell 命令在 hbase 操作中可以起到很大的作用,且主要体现在建表的过程中,看 下面几个 create 属性 1. BLOOMFILTER 默认是 NONE 是否使用布 ...

  2. no.random.randn

    numpy中有一些常用的用来产生随机数的函数,randn就是其中一个,randn函数位于numpy.random中,函数原型如下: numpy.random.randn(d0, d1, ..., dn ...

  3. mariadb密码问题

    错误信息: Mysql:ERROR 1698 (28000): Access denied for user 'root'@'localhost' 解决办法: sudo cat /etc/mysql/ ...

  4. c++——深拷贝和浅拷贝

    深拷贝和浅拷贝 默认复制构造函数可以完成对象的数据成员值简单的复制 对象的数据资源是由指针指示的堆时,默认复制构造函数仅作指针值复制 1浅拷贝问题 1.c++默认的拷贝构造函数 2.=号操作符 都是浅 ...

  5. 【转】如何在VMware上安装macOS Sierra 10.12

    本文主要介绍目前网络上比较流行的使用预安装镜像安装macOS 10.12的方法,并以9月20号发布的最新GM版本16A323为例. 安装方案 破解VMware 创建虚拟机,加载预安装镜像 初始化mac ...

  6. Ajax的async属性

    Ajax请求中的async:false/true的作用 官方的解释是:http://api.jquery.com/jQuery.ajax/ async Boolean Default: true By ...

  7. bat 数组实现

    bat中没有数组的概念,可以通过有[]的多个变量来存储一组值 @echo off & setlocal enabledelayedexpansion .txt) do ( echo %%b e ...

  8. 转换CLOB字段类型为VARCHAR2, lob类型不支持的sql语句

    转自:https://blog.csdn.net/e_wsq/article/details/7561209 步骤: 1.建立一个临时varchar2字段用来保存数据 2.将clob的内容截取后更新到 ...

  9. 如何给sublime text.安装插件

    1.Sublime的Package Control安装方法 Package Control插件本身是一个为了方便管理插件的插件 第一步:ctrl+~,在弹出的下部边框中输入: 如果是 Sublime ...

  10. Android TV 全屏无标题

    想要全部窗口全屏无标题,修改 res\values\styles.xml 可设置主题和样式 <resources> <!-- Base application theme, depe ...