天天看點

[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這樣的字元串,參見代碼如下:

解法一:

解法二:

解法三: