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 :frowning:


<?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

Hi there,

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

Try this instead:

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

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:

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

You can test it here: http://leaverou.github.io/regexplained/

Hi

Thanks for your reply.

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

This should work:

^(?:\\+|(?: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

HI Perhaps you missed my post #4 http://www.sitepoint.com/forums/showthread.php?1159792-Regular-Expression&p=5548533&viewfull=1#post5548533

Oops, I did.
Sorry.

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

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.

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

Well that, would be this:

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

Matches:
+95215487511
0041587695157

Doesn’t match:
+00654718529

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.

So… I’m pretty sure that the pattern FONT=Courier New{2}[/FONT] is equivalent to the pattern 00… that is, exactly two zeros. :wink:

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

Great explanation.
Nice one!

Oops, it certainly is. Well caught.

Matches 000213256498785 which is incorrect :frowning:

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

This should work:

^(?:\\+(?!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”