193. Valid Phone Numbers

来源:互联网 发布:内存优化级别禁用好吗 编辑:程序博客网 时间:2024/05/22 02:12


https://leetcode.com/problems/valid-phone-numbers/description/

For example, assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890
(001) 345-00001

Your script should output the following valid phone numbers:

987-123-4567
(123) 456-7890


#!/bin/bash

reg1="^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
reg2="^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$"
filename="file.txt"
while read line
do
        if [[ $line =~ $reg1 ]] || [[ $line =~ $reg2 ]]
        then
                echo $line
        fi

done < $filename