PHP正则表达式问题?
阿神
阿神 2017-05-16 13:07:23
0
1
376

当我使用正则表达式的时候发现了很诡异的现象,如下:

  • [­--\ˇ]

  • [­_-\ˇ]

  • [­=-\ˇ]

就是第一个不能匹配字母,第二个能匹配字母,第三个能匹配字母和斜杠

我只是希望在字符串里出现这几个特殊字符就替换,没想到连字母都替换了,我知道能用其他写法实现此需求,但我就是不知道这样写为什么会这么诡异,大神们解释下这个正则。

卧槽还有这几个,写个正则这么费劲儿

  • [+-~­­]

  • [i-~­­]

  • [--~­­]

  • [_-~­­]

  • [=-~­­]

  • ...

感兴趣就和hello w\o\r\l\d匹配测试一下。

为什么会这样?

阿神
阿神

闭关修行中......

reply all(1)
我想大声告诉你

First of all, the first example should not be a legal regular, so there will be no result.

The regular expressions in the following two examples are the most basic [a-z] syntax, which means matching all characters from one character to another character encoding.

In the two examples, the starting character is _=, 终止字符都是全角符号 ˇ.
Full-width symbol encoding is greater than all ASCII encodings. All are matched when matching AscII encoding. Without thinking too much, let’s check the ASCII encoding of the starting character.

ASCII encoding:
/ < 0-9 < = < A-Z < < _ < a-z

So, [=-ˇ] 能匹配大写字母、反斜杠、小写字母,[_-ˇ] can only match lowercase letters, neither can match forward slashes and numbers.


Secondly, it is better not to write such a strange regular expression, as it is difficult to maintain.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!