开发者

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

开发者 https://www.devze.com 2023-02-12 06:49 出处:网络
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;

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.


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


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


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


[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


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.


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"


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消