简体   繁体   English

PHP:我不明白为什么此正则表达式与此表达式不匹配

[英]PHP: I don't understand why this regex does not match this expression

I would like to understand what this regex correspond to: 我想了解此正则表达式对应的内容:

preg_match("/\/[A-z]{2}\/[^\/]*/", "expression_to_test")

I've tried with /a2/ but it does not match. 我试过/ a2 /,但不匹配。 As I understand the regex, in this order we have: "/" which is a delimiter, "/" stands for backslash, [Az]{2} stands for 2 letters, then another "/" then no "/" multiple times... 据我了解正则表达式,按此顺序,我们有:“ /”是定界符,“ /”代表反斜杠,[Az] {2}代表2个字母,然后是另一个“ /”,然后没有“ /”多次...

So, it's obviously not the good answer, if you find a matching pattern I would be great. 因此,显然,这不是一个好答案,如果您找到匹配的模式,我会很棒。

Thanks 谢谢

a2 wont match az cause you have decimal number in there. a2不会匹配az因为您在那里有十进制数字。 Try this: 尝试这个:

\/[A-Za-z0-9]{2}\/[^\/]*

https://regex101.com/r/iWSrPD/1 https://regex101.com/r/iWSrPD/1

A-z a single character in the range between A (ASCII 65) and z (ASCII 122) (case sensitive)

2 does not fall in it and so it fails.(as ascii for 2 is 50 ) 2没有掉进去所以失败了。( 2 ascii是50

/Ab/ matches. /Ab/匹配。

See demo. 参见演示。

https://regex101.com/r/woJSXd/1 https://regex101.com/r/woJSXd/1

See in table. 见表。 Only from 65 to 122 仅从65122

You need to use [A-Za-z] as [Az] can match much more than intended.See table. 您需要使用[A-Za-z]因为[Az]可能比预期的要匹配得多。请参见表。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM