I'm trying to figure out how to write my own regex.
I made a list of viable phone numbers and non-viable ones and trying to make sure the viable ones are included but I can't figure out how to finish it up.
Allowed list
0665363636 //
06 65 36 36 36 //
06-65-36-36-36 //
+33 6 65 36 36 36
Not allowed
06 65 36 36 //
2336653636 //
+3366536361 //
0065363636
I messed around with it a bit and I currently have this:
[0+][63][6 \-3][56\ ][\d{1}][\d \-]\d{2}[\d{1} \-]\d\d? ?\-?\d?\d? ?\d?\d?$
This blocks out number 2 and 4 of the non allowed but I can't seem to figure out how to block the other ones out.
Should I put a minimum amount of numbers? If so how would I do this.
Looks like you want to limit the allowed phone numbers to French mobile phone numbers only.
You made a list of valid and invalid strings, which is a good starting point. But then, I think you just wanted to write the pattern in one shot, which is error-prone.
Let's follow a simple methodology and go through the allowed list and craft a very simple regex for each one:
0665363636 -> ^06\d{8}$
06 65 36 36 36 -> ^06(?: \d\d){4}$
06-65-36-36-36 -> ^06(?:-\d\d){4}$
+33 6 65 36 36 36 -> ^\+33 6(?: \d\d){4}$
So far so good.
Now, just combine everything into one regex, and factor it a bit (the 06
part is common in the first 3 cases):
^06(?:\d{8}|(?: \d\d){4}|(?:-\d\d){4})|\+33 6(?: \d\d){4}$
Et voilĂ . Demo here.
As a side note, you should rather use:
^0[67](?:\d{8}|(?: \d\d){4}|(?:-\d\d){4})|\+33 [67](?: \d\d){4}$
As French mobile phone numbers can start in 07 too.