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
The character class [-a-z0-9_-]
is same as [a-z0-9_-]
or [-a-z0-9_]
.
One of the -
can be dropped.
The character was just duplicated. You can remove one instance of it and the regex should work fine.