# Thread: Regular Expression

1. ## Regular Expression

HI

I am trying to write a regular expression that should match the following cases

1. Should start with either + or 00
2. Should be numbers only and length between 11 - 16 chars (length excluding + and 00)

Which means the following should be valid:
+95215487511
0041587695157

My following expression does not work fine

PHP Code:
``` <?php \$reg ="/^[\+|00]{1}[0-9]{11,16}\$/"; \$number = '065471852956'; \$x = preg_match( \$reg , \$number); var_dump(\$x); // returns true ```
Any help is appreciated

Thanks

2. Hi there,

The problem is in your character class.
You cannot write [\+|00]

Code:
`^[\+|0]0?\d{1,11}\$`

3. Actually, thinking about it, my previous answer will match +095215487511 (note the zero after the plus), which is probably not desired.

Here's a better version:

Code:
`^(?:\+|(?:0){2})\d{1,11}\$`
You can test it here: http://leaverou.github.io/regexplained/

4. Hi

Can it also check for overall minimum char length should be 12 ?

I mean the overall case to be something like this

1. Should start with either + or 00
2. After p#1, it should have numbers only and length between 11 - 16 chars
3. Overall length including pt#1 and pt#2 should be minimum 12 chars and max 17 chars

Which means the following should be valid:
+95215487511
0041587695157

and not validate this:
+00654718529

5. This should work:

Code:
`^(?:\+|(?:0){2})\d{11,16}\$`
It checks for either a plus or two zeros
It then checks for a single digit repeated between eleven and sixteen times

6. Oops, I did.
Sorry.

So the initial plus cannot be followed by a zero. Is that correct?

7. It can but we have to assume it into 2 groups

Group 1: Can be either + or 00

Group 2: Should have minimum 11 and max 16 chars

and once validated, the length of Group 1 and Group 2 should be minimum 12 chars.

8. Originally Posted by cancer10
It can but we have to assume it into 2 groups

Group 1: Can be either + or 00

Group 2: Should have minimum 11 and max 16 chars

and once validated, the length of Group 1 and Group 2 should be minimum 12 chars.
Then surely, this will validate:

+00654718529

Group one is a plus.
Group two is eleven digits.
The total length is twelve characters.

9. Yes you are right, if there is + then it cannot be followed by zero, and if there are 00 then it cannot have + at beginning.

Thanks

10. Well that, would be this:

Code:
`^(?:\+(?!0)|(?:0){2})\d{11,16}\$`
Matches:
+95215487511
0041587695157

Doesn't match:
+00654718529

11. Many thanks for your help....seems to be working so far with all the test i made...

would u mind exploding the regex and explain me wots happening so that i can modify it in future if required.

Thanks

12. Originally Posted by cancer10
Many thanks for your help....seems to be working so far with all the test i made...

would u mind exploding the regex and explain me wots happening so that i can modify it in future if required.

Thanks

I'll have a go.

^(?:\+(?!0)|(?:0){2})\d{11,16}\$

^ means from the start of the string, not in the middle or something.

(?:\+(?!0)|(?:0){2})

Brackets have two uses. One to group bits of the expression together, the other to capture the match into a memory. ?: at the start of a bracketed bit turns off the memory aspect of that particular bracket bit.

\+ simply means match a plus sign (a plus sign not proceeded by \, regex's escape character, means something else). The (?!0) bit following on says "not followed by a zero".
So \+(?!0) matches: a + not followed by a zero

| means "or": \+(?!0) OR (?:0){2} -- the brackets containing the | mark the limit of the or.

(?:0){2} - the ?: again turns off the memory aspect of that bracket set. 0 is literally a zero. {2} says two. That is, match two zeros.

\d{11,16} - \d means 1 digit, 0 to 9. With {11,16} after it, it says match between (inclusively) 11 and 16 digits.

\$ means up to the end of the string, not up to the middle or something - no junk after the last bit of the match, 11-16 digits in this case.

13. So... I'm pretty sure that the pattern (?:0){2} is equivalent to the pattern 00... that is, exactly two zeros.

^(?:\+(?!0)|00)\d{11,16}\$

14. Originally Posted by johnyboy
I'll have a go ...
Great explanation.
Nice one!

15. Originally Posted by Jeff Mott
So... I'm pretty sure that the pattern (?:0){2} is equivalent to the pattern 00... that is, exactly two zeros.
Oops, it certainly is. Well caught.

16. Originally Posted by Pullo
Well that, would be this:

Code:
`^(?:\+(?!0)|(?:0){2})\d{11,16}\$`
Matches:
+95215487511
0041587695157

Doesn't match:
+00654718529
Matches 000213256498785 which is incorrect

17. Originally Posted by cancer10
Matches 000213256498785 which is incorrect
Why is it incorrect?

You stated previously:

Originally Posted by cancer10
Group 1: Can be either + or 00
Group 2: Should have minimum 11 and max 16 chars
Part 1: 00
Part 2: 0213256498785

Part two is 13 characters which is fine.

Would you like to exclude the initial 00 being followed by a further 0?

18. Originally Posted by Pullo
Why is it incorrect?

You stated previously:

Part 1: 00
Part 2: 0213256498785

Part two is 13 characters which is fine.

Would you like to exclude the initial 00 being followed by a further 0?

Part 2 cannot start with 0 or 00 or 000 and so on in any case..

Thanks

19. This should work:

Code:
`^(?:\+(?!0)|00)[1-9]\d{10,15}\$`
In this case I am making the first character of part 2 a digit 1 - 9 (i.e. excluding zero), then reducing the number of digits (\d) that may follow to between 10 and 15.

Matches:
+95215487511
0041587695157

Doesn't match:
+00654718529
000213256498785

@Jeff Mott @johnyboy
Do you think that this is the best/cleanest way to go about things?
Ideally I would like to write "any digit, where the first digit is not zero, 11 to 16 times"

20. Originally Posted by Pullo
This should work:

Code:
`^(?:\+(?!0)|00)[1-9]\d{10,15}\$`
In this case I am making the first character of part 2 a digit 1 - 9 (i.e. excluding zero), then reducing the number of digits (\d) that may follow to between 10 and 15.

Matches:
+95215487511
0041587695157

Doesn't match:
+00654718529
000213256498785

@Jeff Mott @johnyboy
Do you think that this is the best/cleanest way to go about things?
Ideally I would like to write "any digit, where the first digit is not zero, 11 to 16 times"

Seems to be working fine so far, i will keep testing for more combinations and shall update this thread if i encounter an issue

many thanks for your help

21. Hi there,

It'll work fine for the conditions we have specified.
It just seemed a little hacky to me, so I wondered if there was a more elegant way of doing it.

22. ^(?:\+(?!0)|00)[1-9]\d{10,15}\$

Seems fine to me apart from I think (?!0) is redundant now? So:

^(?:\+|00)[1-9]\d{10,15}\$

23. Oh yeah, good one.
That was there to make sure that the plus was not followed by a zero, which after the latest modification it won't be anyway.

24. or:

^(?:\+|00)(?!0)\d{11,16}\$

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•