Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex match string of exactly 6 numbers in text that has telephone numbers as well

Tags:

regex

php

I have blocks of free text that contain phone numbers and multiple 6 digit numbers that I need to capture. The 6 digit number has an optional ','.

examples of the 6 digit numbers could be 123456, or 123,456, but I need to differentiate from a phone number like 1 234 456 8901

I have :

    preg_match_all(",\[\W_][0-9]{3}(?:,)[0-9]{3}[\W_][\D]\d",$html, $value); 

Is there a better way to do this?

like image 812
user1592380 Avatar asked Nov 02 '22 15:11

user1592380


1 Answers

It's a bit difficult to review the regex without the sample input but couple of observations:

  • [0-9] can be replaced with \d (since, you're already using it at the end)

  • [\D] is exactly the same as \D. It's a character class itself and unless you have some more characters to include it'ss fine without being enclosed in [].

  • (?:,) should simply be , because you neither want to capture it nor it has any quantifiers.

  • ,\[\W_] Here it seems you want to use the character class but the \ would escape the first [. If you actually need a literal \ there; you need to escape it as \\ since it's a special character.

like image 182
Ravi K Thapliyal Avatar answered Nov 14 '22 07:11

Ravi K Thapliyal