Skip to content

why doesn't \x89 (for example) match the literal byte value 0x89? #1054

Answered by BurntSushi
JackLiar asked this question in Q&A
Discussion options

You must be logged in to vote

You need to disable Unicode mode. That can be done with (?-u) in the pattern itself:

use regex::bytes::Regex;

fn main() {
    let data = b"\xab\x00\x00\x08\x76\x89\x12";
    let re = Regex::new(r"(?-u)\xab\x00.{2}").unwrap();
    let r = re.find(data);
    println!("{:?}", r);

    let re = Regex::new(r"(?-u)\xab\x00..").unwrap();
    let r = re.find(data);
    println!("{:?}", r);

    let re = Regex::new(r"(?-u)\xab\x00.").unwrap();
    let r = re.find(data);
    println!("{:?}", r);

    let re = Regex::new(r"(?-u)\xab\x00\x00").unwrap();
    let r = re.find(data);
    println!("{:?}", r);

    let re = Regex::new(r"(?-u)\xab").unwrap();
    let r = re.find(data);
    println!("{:?}", r)

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by JackLiar
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants