[LeetCode] 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.

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

考察Linux查找文本的操作,主要是命令与正则表达式

# Read from the file file.txt and output all valid phone numbers to stdout.
# No.1
grep -e "^[0-9]\\{3\\}-[0-9]\\{3\\}-[0-9]\\{4\\}$" -e "^([0-9]\\{3\\}) [0-9]\\{3\\}-[0-9]\\{4\\}$" file.txt

# No.2
grep -e "^\\([0-9]\\{3\\}-\\|([0-9]\\{3\\}) \\)[0-9]\\{3\\}-[0-9]\\{4\\}$" file.txt

原文地址:https://www.cnblogs.com/immjc/p/8207207.html

时间: 2024-11-03 12:10:09

[LeetCode] Valid Phone Numbers的相关文章

[LeetCode] 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

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

193. 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 f

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

LeetCode: Add Two Numbers 题解

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

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

[leetcode]_Add Two Numbers

题目:两个链表存储数字,然后求和,和值存储在一个链表中. 代码: 1 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 2 ListNode head = new ListNode(0); 3 ListNode result = head; 4 5 int carry = 0 , tempSum = 0; 6 while(l1 != null || l2 != null){ 7 int v1 , v2; 8 v1 = (l1 !=

LeetCode: Valid Parentheses [020]

[题目] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]

LeetCode: Valid Palindrome [125]

[题目] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider