简体   繁体   中英

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

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? Plus, the website is in UTF-8.

So is it like you cannot include x00 in range because it is the NULL value or is it something different?

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 demo

When using single quotes, the \\x notation is treated as if it was \\\\x and is handled by the regex engine properly.

[^\\x00-\\x7F]

I found something like this that takes x00. I actually used it for special character detection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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