Page 1 of 1

Any RegEx Experts ?

Posted: Tue May 03, 2011 3:10 pm
by TrumpX
I need a regex that will match if a string does not start with 4 letters

The passed string can have 1 to n characters
however it must have 4 letters at the begining

So:
abcdefg -> no match
abcd -> no match
1bcd -> match
a-cd -> match
123 -> match
!$%? -> match
abc -> match (because at least 4 letters required)

Hope thats clear enough
This has to be done with regex only

I tried stuff like:

Code: Select all

/^[^a-z]{1,4}/
won't work...

Re: Any RegEx Experts ?

Posted: Tue May 03, 2011 6:40 pm
by callumacrae
Try this:

Code: Select all

/^[a-zA-Z]{4,}/
It returns the exact opposite of what you want, so use it like this:

Code: Select all

if (!preg_match('/^[a-zA-Z]{4,}/', $string))
{
	//blablabla

Re: Any RegEx Experts ?

Posted: Thu May 05, 2011 1:16 am
by AGC
Callum95 wrote:Try this:

Code: Select all

if (!preg_match('/^[a-zA-Z]{4,}/', $string))
{
	//blablabla
Nice. I'll add something.

To find three letters add this to the if statement.
preg_match('/[a-Z]{1,3}/', $string)

Re: Any RegEx Experts ?

Posted: Thu May 05, 2011 5:38 am
by callumacrae
AGC wrote:
Callum95 wrote:Try this:

Code: Select all

if (!preg_match('/^[a-zA-Z]{4,}/', $string))
{
	//blablabla
Nice. I'll add something.

To find three letters add this to the if statement.
preg_match('/[a-Z]{1,3}/', $string)
He said that it has to be four letters, and yours searches for 1 to 3 letters :/

Re: Any RegEx Experts ?

Posted: Thu May 05, 2011 10:22 am
by TrumpX
I don't have the possibility to negate the matching result
The match has to occur within the expression itself.

I solved the problem like this

Code: Select all

/^(.{0,4}$|.{0,3}[^a-z])/

Re: Any RegEx Experts ?

Posted: Thu May 05, 2011 10:29 pm
by AGC
Callum95 wrote: He said that it has to be four letters, and yours searches for 1 to 3 letters :/
TrumpX wrote:abc -> match (because at least 4 letters required)
He gave examples. :)
TrumpX wrote: I don't have the possibility to negate the matching result

Code: Select all

/^(.{0,4}$|.{0,3}[^a-z])/
You right if you want to store the matches. ;)
Then you get wrong results. 8-)

Re: Any RegEx Experts ?

Posted: Fri May 06, 2011 12:24 pm
by T0ny
TrumpX wrote: I solved the problem like this

Code: Select all

/^(.{0,4}$|.{0,3}[^a-z])/
Should that not be

Code: Select all

/^(.{0,3}$|.{0,3}[^a-z])/
otherwise it would match on abcd

Re: Any RegEx Experts ?

Posted: Fri May 06, 2011 4:52 pm
by TrumpX
T0ny wrote:
TrumpX wrote: I solved the problem like this

Code: Select all

/^(.{0,4}$|.{0,3}[^a-z])/
Should that not be

Code: Select all

/^(.{0,3}$|.{0,3}[^a-z])/
otherwise it would match on abcd
You are right. Thanks

Re: Any RegEx Experts ?

Posted: Wed Jun 01, 2011 9:26 pm
by 1234homie
You dont need to use regex for that. preg_* functions is slow.. try strlen

Re: Any RegEx Experts ?

Posted: Wed Jun 01, 2011 9:38 pm
by Erik Frèrejean
How did you plan to use strlen for this?

Also keep in mind:
TrumpX wrote:This has to be done with regex only

Re: Any RegEx Experts ?

Posted: Thu Jun 02, 2011 9:15 am
by callumacrae
1234homie wrote:You dont need to use regex for that. preg_* functions is slow.. try strlen
I think you mean strstr or strpos ;)

No, it could not be done with either of them, for several reasons:
  • It has to be a regex, as the criteria specifies that it must not start with 4 letters. If you used strstr for this, you could have to execute it 456 976 times (26^4) minimum. That wouldn't be too efficient :P
  • By "I don't have the possibility to negate the matching result", I'm guessing that means that he can only edit the regex itself, and not the code.
~Callum