CodeIgniter Forums

Full Version: Regex help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]gigas10[/eluser]
Can someone please help me, my regular expression is not doing what I need it to do.
What I need is 4 characters, a-z uppercase or lowercase, followed by at least 1 digit to inifinite amount of digits. Current I have:
Code:
/^\w{4}\d+$/

It's weird, test cases I've tried:

ssss2 - Succeed
ssss2222 - Succeed
s2 - Fail
sss2 - Fail
s2323424 - Succeed

Seems its treating \w to work even if digits are supplied, can anyone help me? Thank you.

El Forum

[eluser]danmontgomery[/eluser]
instead of \w use [A-Za-z]

El Forum

[eluser]gigas10[/eluser]
I thought I had tried that, apparently there is a big difference in putting A-Za-z inside of () brackets compared to [] brackets. Thanks for the help.

El Forum

[eluser]Jamie Rumbelow[/eluser]
Just for clarification gigas, () tries to match everything it's contents. Square brackets [] specify a range of characters. If you don't know the difference, it can get pretty confusing!

Jamie

El Forum

[eluser]Sean Gates[/eluser]
For reference in this thread: if you are using a mac there is a great tool called RegExhibit. Works beautifully to quickly test and edit you regular expressions.

El Forum

[eluser]Yorick Peterse[/eluser]
Untested, but should do the trick:

Code:
/^[a-zA-Z]{4}[0-9]+$/