Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grep Match Phones Surrounded by Text

I am trying to locate all phone numbers across various files, including JSON and TXT.

Matching should be done based on whether there are 10 or 11 numeric characters (0-012-345-6789) or (012-345-6789), NOT more and NOT less. The phone numbers are often surrounded by text, but sometimes by spaces and tabs (see below examples). The phone numbers sometimes also include hyphens "-" and parentheses "()" to delineate the numbers.

abc0123456789def <- match
abc10123456789def <- match
abc10123456789def <- match
abc101234567899def <- no match (12 numbers)
abc101234567def <- no match (9 numbers)

abc 0123456789 def <- match
abc 10123456789 def <- match

abc1(012)345-6789def <- match
abc1-012-345-6789def <- match
abc(012)345-6789def <- match
abc012-345-6789def <- match
abc 1(012)345-6789 def <- match

Your help is super appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If I recall grep correctly then:

grep -iP "(?:^|(?<=\D))(?:[\(\)-]?[0-9][\(\)-]?){10,11}(?=\D|$)"
  • (?:^|(?<=\D)) – behind me is the start of the line or a non-digit char
  • (?:[\(\)-]?[0-9][\(\)-]?){10,11} – repeat valid phone digit pattern 10 or 11 times
  • (?=\D|$) – ahead of me is a non-digit char or the end of a line

Here it is in PHP https://regex101.com/r/JJXEHV/1

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading