3 个版本
| 0.1.2 | 2024 年 7 月 25 日 |
|---|---|
| 0.1.1 | 2024 年 7 月 25 日 |
| 0.1.0 | 2024 年 7 月 25 日 |
269 在 文本处理 中
360 每月下载
46KB
857 行代码(不包括注释)
wildcard_ex for Rust
这是一个扩展通配符库,允许使用 VB 类似的指定。它使得可以使用通配符简单指定重复任意字符串。
安装 crate
要安装 crate,请运行以下命令。
cargo add wildcard_ex
示例 - 基本用法
只需像下面这样调用 is_match(pattern, str) 函数即可。
use wildcard_ex::{is_match, ex};
fn main() {
// match with wildcard characters ['*', '?', '#', "[...]"]
assert_eq!(is_match("*.txt", "abc.txt"), true);
assert_eq!(is_match("test*.txt", "test1234.txt"), true);
// using Pattern object
let pattern = ex::Pattern::new("*.txt");
assert_eq!(pattern.is_match("abc.txt"), true);
assert_eq!(pattern.is_match("abc.zip"), false);
}
各种模式匹配示例
可以按照以下方式指定通配符模式进行模式匹配。
use wildcard_ex::{is_match_simple, is_match};
fn main() {
// simple pattern matching with wildcard characters ['*', '?', '#']
assert_eq!(is_match_simple("*.txt", "abc.txt"), true);
assert_eq!(is_match_simple("a???.txt", "abcd.txt"), true);
assert_eq!(is_match_simple("zip:###-####", "zip:111-2222"), true); // '#' is number
// wildcard "[...]"
assert_eq!(is_match("[a-z]1234.txt", "a1234.txt"), true);
assert_eq!(is_match("[a-z][0-9].txt", "b5.txt"), true);
// not pattern
assert_eq!(is_match("[!0-9][0-9].txt", "c9.txt"), true);
// repeating pattern
assert_eq!(is_match("[+0-9].txt", "12345.txt"), true);
assert_eq!(is_match("[+a-z0-9].txt", "abc12345.txt"), true);
// selector
assert_eq!(is_match("[=cat|dog].txt", "cat.txt"), true);
}
is_match_simple指定通用通配符。is_match指定扩展通配符。
通配符模式
下表列出了支持的通配符模式。
| 模式 | 说明 |
|---|---|
| * | 任意字符重复 0 次或多次 |
| ? | 任意单个字符 |
| # | 任意单个数字 (=[0-9]) |
| \ | 转义字符。'\t' 表示制表符,'\n' 表示换行符,'\[' 表示 '[' |
| [str] | 指定字符串 str 中的任意单个字符 |
| [!str] | 不在指定字符串 str 中的任意单个字符 |
| [+str] | 指定字符串 str 中的任意字符重复 1 次或多次 |
| [-str] | 不在指定字符串 str 中的任意字符重复 1 次或多次 |
| [=aaa|bbb] | 字符串 aaa 或 bbb |
- 在
str中,可以使用 \xHH 或 \uHHHH 指定字符代码。
从开头提取匹配部分
use wildcard_ex::*;
fn main() {
assert_eq!(extract_match("*.txt", "abc.txt"), Some("abc.txt".to_string()));
assert_eq!(extract_match("hello*", "hello, world!"), Some("hello, world!".to_string()));
}
(ja) 拡張ワイルドカード
このクレートは、VBライクな指定が可能な拡張ワイルドカードのライブラリです。 簡単な指定でワイルドカードの任意文字列の繰り返し表現が可能です。 日本語などのマルチバイト文字列も問題なく処理できます。
指定可能なのは次のようなワイルドカードのパターンです。
| パターン | 説明 |
|---|---|
| * | 任意の文字が0回以上繰り返される |
| ? | 任意の1文字 |
| # | 任意の1桁の数字 (=[0-\9]) |
| \ | エスケープ文字。'\t'はタブ、'\n'は改行、'\['は'['を意味する |
| [str] | 指定された文字列strのいずれか1文字 |
| [!str] | 指定字符串str以外的任意一个字符 |
| [+str] | 指定字符串str中的任意字符重复一次以上 |
| [-str] | 指定字符串str以外的字符重复一次以上 |
| [=aaa|bbb] | 字符串aaa或bbb |
- [
str]中,可以指定字符编码为\xHH或\uHHHH。
简单用法
is_match_simple用于指定通用通配符。is_match用于指定扩展通配符。
use wildcard_ex::{is_match_simple, is_match};
fn main() {
// simple pattern matching with wildcard characters ['*', '?', '#']
assert_eq!(is_match_simple("*.txt", "abc.txt"), true);
assert_eq!(is_match_simple("a???.txt", "abcd.txt"), true);
assert_eq!(is_match_simple("zip:###-####", "zip:111-2222"), true); // '#' is number
// wildcard "[...]"
assert_eq!(is_match("[a-z]1234.txt", "a1234.txt"), true);
assert_eq!(is_match("[a-z][0-9].txt", "b5.txt"), true);
// not pattern
assert_eq!(is_match("[!0-9][0-9].txt", "c9.txt"), true);
// repeating pattern
assert_eq!(is_match("[+0-9].txt", "12345.txt"), true);
assert_eq!(is_match("[+a-z0-9].txt", "abc12345.txt"), true);
// selector
assert_eq!(is_match("[=cat|dog].txt", "cat.txt"), true);
}