I would like to get the phone numbers from a file. I know the numbers have different forms, I can handle for a single one, but don\'t know how to get a uniform regex. For ex
This is just a modified version of Alan Moore's solution. This is protected against some race condition where the last part of the number has more than four digits in it or the if the total number of digits are more than 10:
grep '\(\(([0-9]\{3\})\|[0-9]\{3\}\)[ -]\?\)\{2\}[0-9]\{4\} '
Explanation:
\(([0-9]\{3\})\|[0-9]\{3\}\) matches exactly three digits (e.g. 234)
with or without surrounded by parentheses. \| performs the 'OR' operation. \( ... \) groups together the above format followed by a space or - or no space at all - ([ -]\?) does that.\{2\} matches exactly two occurrences of the above[0-9]\{4\} ' matches exactly one occurrence for a 4 digit number followed by a space And it's a bit shorter as well. Tested on RHEL and Ubuntu. Cheers!!