Personal Loans - Loans - Refinance - Loans - Free Sprint Ringtones

PDA

View Full Version : Regular Expression Help


x11joex11
Jan 27th 2008, 8:30 pm
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

mrmaypole
Jan 27th 2008, 8:50 pm
Why not just
<?php
if ( strlen( $string ) != 6 ) {
echo "yay";
}
?>

mrmaypole
Jan 27th 2008, 8:56 pm
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

x11joex11
Jan 27th 2008, 9:35 pm
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 27th 2008, 10:56 pm
Hey I found the answer! =) took me a good 3 hours to find the obvious lol.

Here it is!

^.....$|^....$|^...$|^..$|^.$|........*

joebert
Jan 28th 2008, 2:00 am
^.{1,5}$|^.{7,}$

x11joex11
Jan 28th 2008, 9:27 am
Thanks for the help joebert =).