#regex #path #string #expression #regular #turn #user-name

path_to_regexp

将路径字符串(如 /user/:name)转换为正则表达式

3 个版本

0.1.2 2019 年 6 月 3 日
0.1.1 2019 年 4 月 6 日
0.1.0 2019 年 3 月 24 日

#51 in #turn

MIT 许可证

12KB
264 代码行,不包括注释

path_to_regexp

将路径字符串(如 /user/:name)转换为正则表达式

使用示例

extern crate path_to_regexp;

use path_to_regexp::*;

fn main() {
    let scheme: &str = "/route/:foo/:bar/:id";
    let uri: &str = "/route/john/doe/7";
    
    let containers = parse(scheme, Options::default());
    let regexp = to_regexp(containers.as_ref(), Options::default());
    let matches = match_str(uri, regexp, containers.to_vec());

    println!("matches {:#?}", matches);

    /* OUTPUT:
    *    matches [
    *        Match {
    *            name: "foo",
    *            value: "john"
    *        },
    *        Match {
    *            name: "bar",
    *            value: "doe"
    *        },
    *        Match {
    *            name: "id",
    *            value: "7"
    *        }
    *    ]
    */
}

致谢

此包受到其 JavaScript 版本 path-to-regexp 的极大启发。

依赖项

~5–13MB
~147K SLoC