简体   繁体   English

为什么这个正则表达式什么也没找到? x00-xFF

[英]Why does this regular expression find nothing? x00-xFF

I was reading through this program update to see what is new and suddenly I see this thing:我正在阅读此程序更新以查看新内容,突然我看到了以下内容:

 if( preg_match("/[\xE0-\xFF][\x80-\xFF][\x80-\xFF]/", $variablino_namerino) )
 {
    //do stuff
 }

I immediately googled preg_match and discovered this wonderful branch of programming techniques working wonders with regular expressions I have never even heard about.我立即在 google 上搜索了 preg_match 并发现了这个奇妙的编程技术分支,它可以用我从未听说过的正则表达式创造奇迹。 Watched a couple of videos and read a couple of documents.观看了几个视频并阅读了一些文件。 Then I started working this through and understood that it might be possible that values present between E0 and FF might not be there, so I changed this expression so it should always find something:然后我开始解决这个问题,并了解到 E0 和 FF 之间存在的值可能不存在,所以我改变了这个表达式,所以它应该总能找到一些东西:

if( preg_match("/[\x00-\xFF][\x00-\xFF][\x00-\xFF]/", $variablino_namerino) )
{
    //do stuff
}

and actually it does not!事实上它没有! So i thought this was the problem , but it starts working after i change the statement to: 所以我认为这是问题所在,但在我将语句更改为:

 if( preg_match("/[\x01-\xFF][\x01-\xFF][\x01-\xFF]/", $variablino_namerino) )
 {
    //do stuff
 }

where x01 is still a control character, right?其中 x01 仍然是控制字符,对吗? Plus, the website is in UTF-8.另外,该网站采用 UTF-8。

So is it like you cannot include x00 in range because it is the NULL value or is it something different?那么是因为 x00 是 NULL 值还是不同的值,所以您不能在范围内包含 x00 ?

A solution is to either double the backslashes or use single quotes when declaring the regex:一种解决方案是在声明正则表达式时将反斜杠加倍或使用单引号:

if( preg_match('/[\x00-\xFF][\x00-\xFF][\x00-\xFF]/', 'text') ) {
 //do stuff
}

See IDEONE demoIDEONE 演示

When using single quotes, the \\x notation is treated as if it was \\\\x and is handled by the regex engine properly.使用单引号时, \\x符号被视为\\\\x并由正则表达式引擎正确处理。

[^\\x00-\\x7F] [^\\x00-\\x7F]

I found something like this that takes x00.我发现这样的东西需要x00。 I actually used it for special character detection.我实际上将它用于特殊字符检测。

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

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