Given a text file file.txt
that contains a 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.
Ravi Vishwakarma
12-Jun-2024Let's write code to validate the phone number in a specific format using regex in C#.
Explanation:
^
asserts the position at the start of the string.\(?
optionally matches an opening parenthesis(
.\d{3}
matches exactly three digits.\)?
optionally matches a closing parenthesis)
.[- ]?
optionally matches a hyphen-
or a space .\d{3}
matches exactly three digits.[-]
matches exactly a hyphen-
.\d{4}
matches exactly four digits.$
asserts the position at the end of the string.Now call this function
Output :