I need a regex to match numbers in the following formats:
0000 000 0000
01 000 0000
234 1 000 0000
0000-000-0000
00000000000
234 000 000 0000
234(000)000-0000
234(000)000 0000
234(000)000 0000
234(000) 000 0000
234(000)000-0000
2340000000000
2340000000000
What I already tried:
$pattern = '/^234[0-9]{11}/';
But it only matches:
23400000000000
Still a regex newbie. Expression should be fully compatible with PHP 8 PCRE and preg_match_all()
. Would appreciate an explanation of the expression as well.
CodePudding user response:
Remove duplicated questions, please.
You can use reg | reg
as or
operator. Probably last section can be combined with first, but now it is clear what is going on:
(^(\ )?234[\( ]?[0-9]{3}\)? ?[0-9]{3}[\- ]?[0-9]{4})|(^[0-9]{4}[\- ]?[0-9]{3}[\- ]?[0-9]{4})|(^01 ?[0-9]{3} ?[0-9]{4})|(^\ 234 1 [0-9]{3} [0-9]{4})
CodePudding user response:
This is will do what you are looking for .. If you go to THIS SITE, TO SEE DEMO AND EXPLAIATION, you can walk through the right column to see HOW it works. Given you are asking so many questions about regex, I might suggest making a concerted effort to learn the way it works.
/^(\ )?(234|0)[0-9]*?.*/