Regular Expression Help

Discussion in 'PHP' started by voodoo709, Feb 23, 2008.

  1. #1
    Hi,

    I wonder if someone can help me out, I need to detect and remove any group of 5 or more numbers matching the conditions below using one or multiple regular expressions.


    If someone could post a solution in the forum that would be great or if it’s a big job contact my via PM and I can pay you for your time.



    Thanks in advance for any help you can give.



    0123456789

    0 1 2 3 4 5 6 7 8 9 for one or multiple spaces

    0-1-2-3-4-5-6-7-8-9

    Or any of the following in between the numbers
    !”£$%^&*()_+<>:mad:;’,./\|`¬


    zero one two three four five six seven eight nine for one or more spaces



    Or any of the following in between the numbers
    !”£$%^&*()_+<>:mad:;’,./\|`¬


    Eg.
    zero – one – two – three – four – five – six – seven – eight – nine
    zero : one : two : three : four : five : six : seven : eight : nine


    Or combination of the above such as

    0 one two three 4 five 6 seven 8 nine

    or

    0 - 1 - two - three - 4 - five - 6 - seven - 8- nine




    Update to clarify what I’m looking for.

    I need the detected group of 5 or more numbers to be replaced by **number removed**
     
    voodoo709, Feb 23, 2008 IP
  2. patlatyj

    patlatyj Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $z="something else 0 1 - two - three - 4 five - 6 - seven - 8- nine something else ";
    $z=preg_replace('#(zero|one|two|three|four|five|six|seven|eight|nine)#','',$z);
    $z=preg_replace('#\d[\d\s-]{2,}#','',$z);
    echo $z;
     
    patlatyj, Feb 23, 2008 IP
  3. voodoo709

    voodoo709 Member

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #3
    Hi patlatyj

    Thanks for your post however it’s not what I was looking for. It’s my fault for not being more specific.



    I need the detected group of 5 for more number to be replaced by **number removed**


    So

    something else 0 1 - two - three - 4 five - 6 - seven - 8- nine something else

    Would become

    something else **number removed** something else
     
    voodoo709, Feb 23, 2008 IP
  4. patlatyj

    patlatyj Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    **number removed** - a number of digits removed or literally text "**number removed**"?
    if latter
    $z=preg_replace('#([\d\s-]|zero|one|two|three|four|five|six|seven|eight|nine){2,}#',' **number removed** ',$z);
     
    patlatyj, Feb 23, 2008 IP
  5. voodoo709

    voodoo709 Member

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #5

    Yes that right, I want to replace the numbers detected with **number removed**


    I tried the expression you posted but doesn’t cover all the conditions I need.



    My knowledge of regular expressions is very limited but I’ve made some modifications to the ones you’ve posted and come up with the one below:

    $z = preg_replace( '/((zero|one|two|three|four|five|six|seven|eight|nine|\d)[(zero|one|two|three|four|five|six|seven|eight|nine|\d)\s-!:£$%^&*()_+{}\[\]~#\|,–.<>\/\\\]){5,}/i',' **Removed** ', $z);


    This detects the following variations:

    0123456789
    0 1 2 3 4 5 6 7 8 9
    0-1-2-3-4-5-6-7-8-9
    1:2:3:4:5:6:7:8:9:0
    0-1-2(3)4-5-6-7(8)9
    zero one two three four five six seven eight nine for one
    zero–one–two–three–four–five–six–seven–eight–nine
    zero:one:two:three:four:five:six:seven:eight:nine
    0 one two three 4 five 6 seven 8 nine
    etc
    
    Code (markup):



    However it doesn’t cover numbers with more than a single instance of !:£$%^&*()_+{}\[\]~#\|,–.<>\/ between them



    0  1  2  3  4  5  6  7  8  9				two or more spaces
    
    zero – one – two – three – four – five – six – seven – eight – nine
    
    zero : one : two : three : four : five : six : seven : eight : nine
    
    zero : one : two : three : four : five : six : seven : eight : nine
    
    zero::one::two::three::four::five::six::seven::eight::nine
    
    0 - 1 - two - three - 4 - five - 6 - seven - 8- nine
    Code (markup):


    It detects numbers with a single space,dot,dash etc but not multiple spaces,dots,dashes etc



    Can the regular expressed be modified to also include the above?



    Thanks agian for any help
     
    voodoo709, Feb 23, 2008 IP
  6. voodoo709

    voodoo709 Member

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #6
    Ok just a quick update, I’ve created the following regular expression which seems to detect all the variations I’ve motioned in the thread.


    I still need to do some more testing with it to ensure it doesn’t match anything by mistake.

    $z = preg_replace( '/((zero|one|two|three|four|five|six|seven|eight|nine|\d)+|[\s-!:£$%^&*()_+{}\[\]~#\|,–.<>\/\\\]+(zero|one|two|three|four|five|six|seven|eight|nine|\d)){5,}/i',' **Removed** ', $z);


    I would appreciate if a regular expression expert could take a look and see if I’ve made any mistakes that could cause a problem.



    Thanks
     
    voodoo709, Feb 23, 2008 IP
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #7
    Insure that you are escaping characters like * ? . / ( ) etc..

    Peace,
     
    Barti1987, Feb 23, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    Does this do it for you ?

    <?php
    $a[] = 'three – four – five – six – seven – eight – nine';
    $a[] = '...: one : two : three : four : five : six : seven : eight :...';
    $a[] = 'two : three : four : five damnit mizzan';
    $a[] = 'zero::one::two::three::four::five::six::seven::eight::nine';
    $a[] = '0 - 1 - two - three - 4 - five - 6 - seven - 8- nine';
    
    foreach($a as $aa)
    {
    	echo preg_replace('#((zero|one|two|three|four|five|six|seven|eight|nine|\d+)[^a-z0-9]+){4,}(zero|one|two|three|four|five|six|seven|eight|nine|\d+)#i', '**Number Removed**', $aa) . '<br/>';
    }
    
    ?>
    PHP:
     
    joebert, Feb 23, 2008 IP
  9. voodoo709

    voodoo709 Member

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #9
    Thanks joebert, that’s almost perfect


    Just one small problem, it detects everything except the following:

    436333
    4-1-43633


    Would it be possible to modify your expression to cover the above or would it be better to use another regular expression to catch the ones that are missed?
     
    voodoo709, Feb 23, 2008 IP
  10. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #10
    This might do it.
    <?php
    $a[] = 'three – four – five – six – seven – eight – nine';
    $a[] = '...: one : two : three : four : five : six : seven : eight :...';
    $a[] = 'two : three : four : five damnit mizzan';
    $a[] = 'zero::one::two::three::four::five::six::seven::eight::nine';
    $a[] = '0 - 1 - two - three - 4 - five - 6 - seven - 8- nine';
    $a[] = '436333';
    $a[] = '4-1-43633';
    
    foreach($a as $aa)
    {
    	echo preg_replace('#((zero|one|two|three|four|five|six|seven|eight|nine|\d)[^a-z0-9]*){4,}(zero|one|two|three|four|five|six|seven|eight|nine|\d)#i', '**Number Removed**', $aa) . '<br/>';
    }
    
    ?>
    PHP:
     
    joebert, Feb 23, 2008 IP
    voodoo709 likes this.
  11. voodoo709

    voodoo709 Member

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #11
    Thanks, that’s perfect.
     
    voodoo709, Feb 23, 2008 IP