Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts

    Regex Question for the advanced users

    I have a regular expression to match numbers in this format

    087xxxxxxx
    086xxxxxxx
    085xxxxxxx
    083xxxxxxx
    (087) xxxxxxx
    (087)xxxxxxx
    ( 087 )xxxxxxx
    ( 087 ) xxxxxxx
    087 xxxxxxx
    087-xxxxxxx
    087 - xxxxxxx

    PHP Code:
    #([( ]{0,2}08[3567])[) -]{0,3}([0-9]{7})# 
    How do i write a regex to take numbers in the above formats but not numbers that have a digit repeated more than 4 times like this
    0879811111

    Thanks

  • #2
    Regular Coder
    Join Date
    Jul 2009
    Location
    Chicago, IL
    Posts
    169
    Thanks
    26
    Thanked 3 Times in 3 Posts
    I'm not sure if you can do that with regexes, but here's what I would do:
    PHP Code:
    // run your regex code here
    // if your string passes the regex, then go on to the code below
    $data "0879811111"// your string
    $limit 4;
    foreach (
    count_chars($data1) as $i => $val)
    {
        if(
    $val $limit)
        {
            echo 
    "Too many instances of \"".chr($i)."\"";
            break;
        }


  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    forget the regex to match, instead use it to clean it up
    PHP Code:
    $data "(087) 9811111";
    $data preg_replace('/[^\d]+/'''$data);
    if(
    strlen($data) != 10) {
        echo 
    'Not enough digits';
    }else{
        
    $chars str_split($data);
        if(
    array_pop(array_keys(array_count_values(array_count_values($chars)))) > 3) {
            echo 
    'Too many repetitive numbers';
        }else{
            echo 
    $data;
        }



  •  

    Posting Permissions

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