-1

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 ?

thangvc91
  • 323
  • 2
  • 10
  • 1
    `^\+?\d{2,4}[ .,]\d{3}[ .,]?\d{3}[ .,]?\d{0,3}$` [DEMO](https://regex101.com/r/gfCvhE/1) – dawg Dec 31 '21 at 16:36
  • `[" ","."]?` is not a thing. You lifelike mean `[ ,.]?` – dawg Dec 31 '21 at 16:37
  • Try `'(\+84 )?\d{3,4}[ ,.]?\d{3,4}[ ,.]?\d{3,4}'` – hyena Dec 31 '21 at 16:41
  • 2
    Try reading through some general introductions to regular expressions, such as [Learning Regular Expressions](https://stackoverflow.com/q/4736/157957) and [What does this regex mean?](https://stackoverflow.com/q/22937618/157957). Alternatively, you could use one of the libraries or patterns listed at [How to validate phone numbers using regex](https://stackoverflow.com/q/123559/157957). – IMSoP Dec 31 '21 at 16:43
  • In order to write a regex to match something, you have to be able to specify in natural language what rules you want it to match. So what are the rules for the phone numbers that you want to match? – Andy Lester Dec 31 '21 at 17:09

1 Answers1

1

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

The fourth bird
  • 154,723
  • 16
  • 55
  • 70