Rust regex pattern - unrecognized escape pattern

前端 未结 3 840
醉话见心
醉话见心 2021-01-21 07:25

I do have following string:

\\\"lengthSeconds\\\":\\\"2664\\\"

which I would like to match with this regexp:

Regex::new(\"lengt         


        
3条回答
  •  难免孤独
    2021-01-21 07:38

    By using r#..#, you treat your string as a raw string and hence do not process any escapes. However, since backslashes are special characters in Regex, the Regex expression itself still requires you to escape backslashes. So this

    Regex::new(r#"\\"lengthSeconds\\":\\"(\d+)\\""#)

    is what you want.

    Alternatively, you could write

    Regex::new("\\\\\"lengthSeconds\\\\\":\\\\\"(\\d+)\\\\\"").unwrap();

    to yield the same result.

    See this example on Rust Playground

提交回复
热议问题