2

I'm extending the CodeIgniter Form Validation library to check for Alpha Numeric values with underscores excluding dashes. But I'm very new to regex patterns and would like some help...

Currently for alpha_numeric CI has:

return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;

And for alpha_dash CI has:

return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;

And I'm creating 'alpha_underscore' (which again will be alpha numeric with underscores):

Is this correct?

return ( ! preg_match("/^([a-z0-9_])+$/i", $str)) ? FALSE : TRUE;

I am confused as to why there is a '-' at the beginning and end of the 'alpha_dash' pattern

1
  • 2
    both answers were correct, but I picked codaddict's (which I read Call of Duty Addict) b/c it was clearer Commented Mar 8, 2011 at 16:12

2 Answers 2

4

The character class [-a-z0-9_-] is same as [a-z0-9_-] or [-a-z0-9_].

One of the - can be dropped.

Sign up to request clarification or add additional context in comments.

Comments

3

The character was just duplicated. You can remove one instance of it and the regex should work fine.

1 Comment

So to clarify, the '-' was duplicated in the 'alpha_dash' pattern and my regex is fine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.