Any RegEx Experts ?

Discussion forum for MOD Writers regarding MOD Development.
Locked
User avatar
TrumpX
Registered User
Posts: 93
Joined: Tue Nov 16, 2010 2:45 pm
Location: Online

Any RegEx Experts ?

Post 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...
User avatar
callumacrae
Former Team Member
Posts: 2662
Joined: Tue Feb 12, 2008 12:28 pm
Location: London, UK
Name: Callum Macrae
Contact:

Re: Any RegEx Experts ?

Post 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
macr.ae = my website. you probably won't like it.
Proud user ofProud user of
User avatar
AGC
Registered User
Posts: 292
Joined: Sat Sep 15, 2007 10:26 pm
Contact:

Re: Any RegEx Experts ?

Post 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)
(phpBB 2.0.x): []Admin Topics List[] MOD. << Description & Download. , []Highlight Author[] MOD.

Donate-paypal
(phpBB 3.0.x)[ver] MODs: [RC1] >Import old *.pak files MOD. + . . .[8] > ACP - Modules Quick Access (MQA) MOD. + . . . [7-PL1] > Multi Smile (actions) MOD
User avatar
callumacrae
Former Team Member
Posts: 2662
Joined: Tue Feb 12, 2008 12:28 pm
Location: London, UK
Name: Callum Macrae
Contact:

Re: Any RegEx Experts ?

Post 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 :/
macr.ae = my website. you probably won't like it.
Proud user ofProud user of
User avatar
TrumpX
Registered User
Posts: 93
Joined: Tue Nov 16, 2010 2:45 pm
Location: Online

Re: Any RegEx Experts ?

Post 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])/
User avatar
AGC
Registered User
Posts: 292
Joined: Sat Sep 15, 2007 10:26 pm
Contact:

Re: Any RegEx Experts ?

Post 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-)
(phpBB 2.0.x): []Admin Topics List[] MOD. << Description & Download. , []Highlight Author[] MOD.

Donate-paypal
(phpBB 3.0.x)[ver] MODs: [RC1] >Import old *.pak files MOD. + . . .[8] > ACP - Modules Quick Access (MQA) MOD. + . . . [7-PL1] > Multi Smile (actions) MOD
User avatar
T0ny
Registered User
Posts: 1383
Joined: Sun Jan 29, 2006 8:42 pm
Location: Lancashire
Name: Tony

Re: Any RegEx Experts ?

Post 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
User avatar
TrumpX
Registered User
Posts: 93
Joined: Tue Nov 16, 2010 2:45 pm
Location: Online

Re: Any RegEx Experts ?

Post 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
User avatar
1234homie
Registered User
Posts: 439
Joined: Fri Sep 26, 2008 3:17 pm

Re: Any RegEx Experts ?

Post by 1234homie »

You dont need to use regex for that. preg_* functions is slow.. try strlen
User avatar
Erik Frèrejean
Former Team Member
Posts: 9899
Joined: Tue Oct 09, 2007 9:09 am
Location: The Netherlands, 3.0.x Support Forum
Name: Erik Frèrejean
Contact:

Re: Any RegEx Experts ?

Post 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
Support Toolkit | Support Request Template | Knowledge Base | phpBB 3.0.x documentation
I don't give support via PM or IM! (all unsolicited pms will be trashed!)
User avatar
callumacrae
Former Team Member
Posts: 2662
Joined: Tue Feb 12, 2008 12:28 pm
Location: London, UK
Name: Callum Macrae
Contact:

Re: Any RegEx Experts ?

Post 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
macr.ae = my website. you probably won't like it.
Proud user ofProud user of
Locked

Return to “[3.0.x] MOD Writers Discussion”