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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With