SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 32
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question 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. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

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

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

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    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. #4
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    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. #6
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Oops, I did.
    Sorry.

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

  8. #8
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by cancer10 View Post
    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.

  10. #10
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Well that, would be this:

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

    Doesn't match:
    +00654718529

  12. #12
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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

  13. #13
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    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.

  14. #14
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,149
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    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}$
    "First make it work. Then make it better."

  15. #15
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by johnyboy View Post
    I'll have a go ...
    Great explanation.
    Nice one!

  16. #16
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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.

  17. #17
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Well that, would be this:

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

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

  18. #18
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Matches 000213256498785 which is incorrect
    Why is it incorrect?

    You stated previously:

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

  19. #19
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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

  20. #20
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    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"

  21. #21
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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

  22. #22
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    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.

  23. #23
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ^(?:\+(?!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}$

  24. #24
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    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.

  25. #25
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    or:

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


Bookmarks

Posting Permissions

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