bobo
bobo

Reputation: 8727

Is [-a-z] the same as [a-zA-Z] in regular expression?

From the CI_Form_validation class in Codeigniter, I see this function:

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

I think the - before a-z possibly means [a-zA-Z], but entering -a-z in google does not give any useful result.

I would like to know if [-a-z] is the same as [a-zA-Z]?

Thanks in advance.

EDIT:

After seeing the i, I now know [-a-z] does not mean [a-zA-Z]. But what is it? There is already a - at the end.

Upvotes: 1

Views: 1282

Answers (7)

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55489

In addition to the answers given by @David and @Ryan, I would also like to add that if you are looking for something that means equal to [a-zA-Z] then it would be /^[a-z]$/i where i means case-insensitive.

Upvotes: 2

Dennis G
Dennis G

Reputation: 21788

[a-z] is not the same as [a-zA-Z], but when you do a case insensitive search, of course it is the same. [-a-z] is simply a hyphen and the lowercase letters a-z, and the /i makes it lowercase + uppercase.

The "i" after the pattern delimiter indicates a case-insensitive search

from preg_match

Upvotes: 2

El Ronnoco
El Ronnoco

Reputation: 11922

[-a-z] will match hyphen (-) or any lowercase letter

[a-zA-Z] will match any lower or uppercase letter

Upvotes: 1

Stofke
Stofke

Reputation: 2958

As far as I know but I'm not a regex expert this is not the same.

[a-zA-Z]

means "Any upper and lowercase alphabetic character"

while

[-a-z]

means "Any minus or lowercase alphabetic character"

Upvotes: 1

jb.
jb.

Reputation: 10341

No, it is not. But your regex uses the i modifier on the end of the regex which signifies ignorecase.

so /[a-z]/i and /[a-zA-Z]/ are the same thing

Upvotes: 7

Ryan Bigg
Ryan Bigg

Reputation: 107728

-a-z would be the hyphen character (-) and any letter between a and z. But then they've got it again later in the expression by itself which is redundant.

Upvotes: 4

Quentin
Quentin

Reputation: 943635

No. It means "A hyphen or any of the letters between a and z".

Upvotes: 7

Related Questions