天天看点

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

Given a text file <code>file.txt</code> 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 <code>file.txt</code> has the following content:

Your script should output the following valid phone numbers:

[0−9]3[0−9]3 )就可以理解了,它匹配了xxx-和(xxx) 这两种形式的字符串,然后后面的就好理解了,匹配xxx-xxxx这样的字符串,参见代码如下:

解法一:

解法二:

解法三: