gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:27:11 25 4
gpt4 key购买 nike

我有一个&[u8],我需要验证它是否符合某种模式。在 Regex documentation 中有在 &[u8] 上使用正则表达式的示例在the module documentation .我从 the examples section 中获取了代码并将其放入 main() 并添加一些声明:

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。一般来说,代码没有暗示它与通常的字符串匹配有何不同,我对后者没有任何问题。

我想我做错了一些基本错误,比如缺少或更精确的导入。我在这里玩猜谜游戏,因为文档未能提供工作示例(他们经常这样做),这次编译器也未能将我推向正确的方向。

编译器信息如下:

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

最佳答案

and added a few declarations

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

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)");
}

as the docs fail to provide working examples (as they regularly do)

请注意,文档中的代码块是默认编译和执行的,因此根据我的经验,示例无法运行的情况非常罕见。

关于regex - 如何在字节(Vec<u8> 或 &[u8])上使用 Rust 正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46491927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com