CodeIgniter Forums
Regex help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Regex help (/showthread.php?tid=26170)



Regex help - El Forum - 01-07-2010

[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.


Regex help - El Forum - 01-07-2010

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


Regex help - El Forum - 01-07-2010

[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.


Regex help - El Forum - 01-07-2010

[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


Regex help - El Forum - 01-07-2010

[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.


Regex help - El Forum - 01-09-2010

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

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