Home > Mobile >  Regular Expression - how to match all phone formating
Regular Expression - how to match all phone formating

Time:01-03

I have some format phone like below:

 84 934.567.678 
 84 934567678
0934 567 678 
0934.567.678

I have written the regular expression to wish it capture all above phone number, however I just got 2 last number , can not capture both line begining with 84

'\d{3,4}[" ","."]?\d{3,4}[" ","."]?\d{3,4}'

Could you please help assist for getting all the phone format ?

CodePudding user response:

You can use an alternation to match both separate formats as {3,4} can match variable formats and possibly unwanted matches.

With a capture group and a backreference \1 you can match up the space and dot.

^(?:(?:\ \d{2} )\d{3}\.?\d{3}\.?\d{3}|\d{4}([ .]?)\d{3}\1\d{3})$

Regex demo

  • Related