1 个不稳定版本
使用旧的Rust 2015
0.1.0 | 2018年5月10日 |
---|
#6 在 #soup
84KB
2K SLoC
remake
一个用于编写可维护正则表达式的Rust库。正则表达式是奇妙的事物,但它们缺乏抽象和模块化的工具。当正则表达式很小时,这并不是一个大问题,但当它们变得更大时,维护它们可能会变得令人沮丧。Remake允许你命名单个正则表达式,就像在传统编程语言中将字符串或数字组合起来一样。当你想使用你熟悉和喜爱的简短正则表达式语法时,你仍然可以这样做,但当你想将事物分解成小块时,这很容易。
文档
模块文档 包含了对remake语言的完整解释,并带有内联示例。
用法
将以下内容添加到您的 Cargo.toml
[dependencies]
remake = "0.1.0"
并将以下内容添加到您的crate根目录
extern crate remake;
以下是一个简单的示例,它构建了一个玩具URI验证器
extern crate remake;
use remake::Remake;
fn main() {
let web_uri_re = Remake::compile(r#"
let scheme = /https?:/ . '//';
let auth = /[\w\.\-_]+/;
let path = ('/' . /[\w\-_]+/)*;
let query_body = (/[\w\.\-_?]/ | '/')*;
let frag_body = cap query_body as frag;
/^/
. scheme . auth . path
. ('?' . query_body)?
. ('#' . frag_body)?
. /$/
"#).unwrap();
assert!(web_uri_re.is_match("https://rust-lang.net.cn"));
assert!(web_uri_re.is_match("https://github.com/ethanpailes/remake"));
assert_eq!(
web_uri_re
.captures("https://tools.ietf.org/html/rfc3986#section-1.1.3")
.unwrap().name("frag").unwrap().as_str(),
"section-1.1.3");
}
依赖项
~3–5MB
~97K SLoC