Regular Expression Help

Discussion in 'PHP' started by x11joex11, Jan 27, 2008.

  1. #1
    I have a question, I am trying to write a RegEX to match only characters that are 1-5 characters in length not 6 and characters greater then 6.

    So for example,

    hey - ok
    there- ok
    muscle - bad (6 characters)
    crazy - ok
    morethensix - ok (above 6 so still good)

    I can write a regEX that does the reverse and only matches '6' characters and it looks like this

    ^.{6}$

    If anyone can help let me know, much appreciated! (I'm probably just being stupid and missing something easy).

    Note** (It must be only a regEX code, you can't change the PHP code to say !pregmatch, it has to do with the custom client system that is in place that is designed to work with positive matches and not negative)

    Best,
    - Joe
     
    x11joex11, Jan 27, 2008 IP
  2. mrmaypole

    mrmaypole Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why not just
    <?php
    if ( strlen( $string ) != 6 ) {
    echo "yay";
    }
    ?>
     
    mrmaypole, Jan 27, 2008 IP
  3. mrmaypole

    mrmaypole Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hey you can do the $ thing to end the string...
    |^.{6}$|

    :cool:
    <?php

    $strings=array( 'ab','abcd','abcdef','abcedefg' );

    foreach( $strings as $string ) {

    echo "testing $string\n";
    echo "strlen: ".strlen( $string )."\n";
    if ( ! preg_match( '|^.{6}$|', $string ) ) {
    echo "pregmatch_1 pass\n";
    }


    }

    ?>

    =
    output:
    testing ab
    strlen: 2
    pregmatch pass
    testing abcd
    strlen: 4
    pregmatch pass
    testing abcdef
    strlen: 6
    pregmatch fail
    testing abcedefg
    strlen: 8
    pregmatch pass
     
    mrmaypole, Jan 27, 2008 IP
  4. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for that help mrmaypole but I was afraid you would do that, notice how you said ! pregmatch, it needs to be the other way around, I know that sounds weird, but because of a system I am using that is already in place to function one way I have to make a regEX and can't change the code, any ideas?

    It's basically a custom library my client made that functions with RegEx Codes, but it calculates what to do with preg_match (not !pregmatch) and I can't change it :(. So it's why I need just only the RegEx code that will perform that job given you are trying to match it in that manner.
     
    x11joex11, Jan 27, 2008 IP
  5. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey I found the answer! =) took me a good 3 hours to find the obvious lol.

    Here it is!

    ^.....$|^....$|^...$|^..$|^.$|........*
     
    x11joex11, Jan 27, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    ^.{1,5}$|^.{7,}$
    Code (markup):
     
    joebert, Jan 28, 2008 IP
  7. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for the help joebert =).
     
    x11joex11, Jan 28, 2008 IP