| Crates.io | wildcard_ex |
| lib.rs | wildcard_ex |
| version | 0.1.2 |
| created_at | 2024-07-25 02:29:34.606119+00 |
| updated_at | 2024-07-25 11:33:48.789311+00 |
| description | This is a library for extended wildcards that allows VB-like specifications. |
| homepage | |
| repository | https://github.com/kujirahand/wildcard_ex-rust |
| max_upload_size | |
| id | 1314617 |
| size | 46,787 |
This is a library for extended wildcards that allows VB-like specifications. It enables the expression of repeating arbitrary strings with simple specifications using wildcards.
To install the crate, run the following command.
cargo add wildcard_ex
You just need to call the is_match(pattern, str) function as shown below.
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);
}
Pattern matching can be performed by specifying wildcard patterns as shown below.
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 specifies general wildcards.is_match specifies extended wildcards.The supported patterns are as follows in the table below.
| Pattern | Description |
|---|---|
| * | Any character repeated 0 or more times |
| ? | Any single character |
| # | Any single digit (=[0-9]) |
| \ | Escape character. '\t' means tab, '\n' means newline, '[' means '[' |
| [str] | Any single character from the specified string str |
| [!str] | Any single character except those in the specified string str |
| [+str] | Any character from the specified string str repeated 1 or more times |
| [-str] | Any character except those in the specified string str repeated 1 or more times |
| [=aaa|bbb] | The string aaa or bbb |
str], you can specify character codes using \xHH or \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()));
}
このクレートは、VBライクな指定が可能な拡張ワイルドカードのライブラリです。 簡単な指定でワイルドカードの任意文字列の繰り返し表現が可能です。 日本語などのマルチバイト文字列も問題なく処理できます。
指定可能なのは次のようなワイルドカードのパターンです。
| パターン | 説明 |
|---|---|
| * | 任意の文字が0回以上繰り返される |
| ? | 任意の1文字 |
| # | 任意の1桁の数字 (=[0-\9]) |
| \ | エスケープ文字。'\t'はタブ、'\n'は改行、'\['は'['を意味する |
| [str] | 指定された文字列strのいずれか1文字 |
| [!str] | 指定された文字列str以外のいずれか1文字 |
| [+str] | 指定された文字列strの任意の文字が1回以上繰り返される |
| [-str] | 指定された文字列str以外の文字が1回以上繰り返される |
| [=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);
}