LeetCode193 solution

Valid Phone Numbers

The original problem is https://leetcode.com/problems/valid-phone-numbers/.

The basic problem here to match lines of telephone numbers in form of

(123) 456-789

This question is relative easy. Pattern matching is other languages such as python, or perl would be even easier.

I am using a BSD bash (that's the version on Mac OS X). Note that I use -E to use the extended regular expression, which allows me to write metacharacters (, ), {,},etc., without escaping them.

Note also the precedence with bash regex: repetition( {x} ) > concatenation > alternation( | )

The solution:

grep  -E '^(\([0-9]{3}\) [0-9]{3}\-[0-9]{4}|[0-9]{3}\-[0-9]{3}\-[0-9]{4})$' file.txt