• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex help

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

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

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

#4
[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

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.