首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在字节(Vec<u8>或&[u8])上使用Rust正则表达式?

如何在字节(Vec<u8>或&[u8])上使用Rust正则表达式?
EN

Stack Overflow用户
提问于 2017-09-29 15:14:58
回答 1查看 1.2K关注 0票数 2

我有一个&[u8],我需要验证它是否符合某种模式。在&[u8]上使用regexes的例子有文档中和模块文档中。我从例句部分获取代码并将其放入main()中,并添加了一些声明:

代码语言:javascript
复制
extern crate regex;
use regex::Regex;

fn main() {
    let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
    let text = b"Not my favorite movie: 'Citizen Kane' (1941).";
    let caps = re.captures(text).unwrap();
    assert_eq!(&caps[1], &b"Citizen Kane"[..]);
    assert_eq!(&caps[2], &b"1941"[..]);
    assert_eq!(&caps[0], &b"'Citizen Kane' (1941)"[..]);
    // You can also access the groups by index using the Index notation.
    // Note that this will panic on an invalid index.
    assert_eq!(&caps[1], b"Citizen Kane");
    assert_eq!(&caps[2], b"1941");
    assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
}

我不明白这个示例代码与常规字符串匹配有什么不同,而且编译器确实抱怨期待一个&str。通常,代码并不暗示它与通常的字符串匹配有何不同,我对此没有任何问题。

我想我做了一些基本错误,比如缺少或更精确的进口。我在这里玩猜谜游戏,因为文档没有提供工作示例(就像它们经常做的那样),而这一次编译器也没能推动我朝正确的方向前进。

下面是编译器消息:

代码语言:javascript
复制
error[E0308]: mismatched types
 --> src/main.rs:7:28
  |
7 |     let caps = re.captures(text).unwrap();
  |                            ^^^^ expected str, found array of 45 elements
  |
  = note: expected type `&str`
             found type `&[u8; 45]`

error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8]>` is not satisfied
 --> src/main.rs:8:5
  |
8 |     assert_eq!(&caps[1], &b"Citizen Kane"[..]);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8]`
  |
  = help: the trait `std::cmp::PartialEq<[u8]>` is not implemented for `str`
  = note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8]>` for `&str`
  = note: this error originates in a macro outside of the current crate

error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8]>` is not satisfied
 --> src/main.rs:9:5
  |
9 |     assert_eq!(&caps[2], &b"1941"[..]);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8]`
  |
  = help: the trait `std::cmp::PartialEq<[u8]>` is not implemented for `str`
  = note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8]>` for `&str`
  = note: this error originates in a macro outside of the current crate

error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8]>` is not satisfied
  --> src/main.rs:10:5
   |
10 |     assert_eq!(&caps[0], &b"'Citizen Kane' (1941)"[..]);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8]`
   |
   = help: the trait `std::cmp::PartialEq<[u8]>` is not implemented for `str`
   = note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8]>` for `&str`
   = note: this error originates in a macro outside of the current crate

error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8; 12]>` is not satisfied
  --> src/main.rs:13:5
   |
13 |     assert_eq!(&caps[1], b"Citizen Kane");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8; 12]`
   |
   = help: the trait `std::cmp::PartialEq<[u8; 12]>` is not implemented for `str`
   = note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8; 12]>` for `&str`
   = note: this error originates in a macro outside of the current crate

error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8; 4]>` is not satisfied
  --> src/main.rs:14:5
   |
14 |     assert_eq!(&caps[2], b"1941");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8; 4]`
   |
   = help: the trait `std::cmp::PartialEq<[u8; 4]>` is not implemented for `str`
   = note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8; 4]>` for `&str`
   = note: this error originates in a macro outside of the current crate

error[E0277]: the trait bound `str: std::cmp::PartialEq<[u8; 21]>` is not satisfied
  --> src/main.rs:15:5
   |
15 |     assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `str` with `[u8; 21]`
   |
   = help: the trait `std::cmp::PartialEq<[u8; 21]>` is not implemented for `str`
   = note: required because of the requirements on the impl of `std::cmp::PartialEq<&[u8; 21]>` for `&str`
   = note: this error originates in a macro outside of the current crate
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-29 15:18:48

并添加了一些声明

不幸的是,你加错了。请注意,您链接到的文档是如何用于结构regex::bytes::Regex,而不是regex::Regex的--它们是两种不同的类型!

代码语言:javascript
复制
extern crate regex;
use regex::bytes::Regex;
//         ^^^^^

fn main() {
    let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
    let text = b"Not my favorite movie: 'Citizen Kane' (1941).";
    let caps = re.captures(text).unwrap();

    assert_eq!(&caps[1], &b"Citizen Kane"[..]);
    assert_eq!(&caps[2], &b"1941"[..]);
    assert_eq!(&caps[0], &b"'Citizen Kane' (1941)"[..]);

    assert_eq!(&caps[1], b"Citizen Kane");
    assert_eq!(&caps[2], b"1941");
    assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
}

因为文档没有提供工作示例(就像他们经常做的那样)

请注意,文档中的代码块是默认编译和执行的,因此我的经验是,这些示例不起作用是非常罕见的。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46491927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档