#fnmatch #glob #pattern #regex

fnmatch-regex

Convert a glob-style pattern to a regular expression

2 unstable releases

0.2.0 Jun 11, 2022
0.1.0 Jun 22, 2021

#678 in Encoding

Download history 266/week @ 2023-10-16 163/week @ 2023-10-23 339/week @ 2023-10-30 169/week @ 2023-11-06 228/week @ 2023-11-13 216/week @ 2023-11-20 201/week @ 2023-11-27 219/week @ 2023-12-04 123/week @ 2023-12-11 127/week @ 2023-12-18 99/week @ 2023-12-25 100/week @ 2024-01-01 294/week @ 2024-01-08 143/week @ 2024-01-15 311/week @ 2024-01-22 196/week @ 2024-01-29

950 downloads per month
Used in 4 crates (3 directly)

BSD-2-Clause

38KB
661 lines

fnmatch-regex - build regular expressions to match glob-style patterns

This crate currently provides a single function, glob_to_regex, that converts a glob-style pattern with some shell extensions to a regular expression. Note that it only handles text pattern matching, there are no attempts to verify or construct any filesystem paths.

The glob-style pattern features currently supported are:

  • any character except ?, *, [, \, or { is matched literally

  • ? matches any single character except a slash (/)

  • * matches any sequence of zero or more characters that does not contain a slash (/)

  • a backslash allows the next character to be matched literally, except for the \a, \b, \e, \n, \r, and \v sequences

  • a [...] character class supports ranges, negation if the very first character is !, backslash-escaping, and also matching a ] character if it is the very first character possibly after the ! one (e.g. []] would only match a single ] character)

  • an {a,bbb,cc} alternation supports backslash-escaping, but not nested alternations or character classes yet

Note that the * and ? wildcard patterns, as well as the character classes, will never match a slash.

Examples:

  • abc.txt would only match abc.txt

  • foo/test?.txt would match e.g. foo/test1.txt or foo/test".txt, but not foo/test/.txt

  • /etc/c[--9].conf would match e.g. /etc/c-.conf, /etc/c..conf, or /etc/7.conf, but not /etc/c/.conf

  • linux-[0-9]*-{generic,aws} would match linux-5.2.27b1-generic and linux-4.0.12-aws, but not linux-unsigned-5.2.27b1-generic

Note that the negation modifier for character classes is !, not ^.

let re_name = fnmatch_regex::glob_to_regex("linux-[0-9]*-{generic,aws}")?;
for name in &[
    "linux-5.2.27b1-generic",
    "linux-4.0.12-aws",
    "linux-unsigned-5.2.27b1-generic"
] {
    let okay = re_name.is_match(name);
    println!(
        "{}: {}",
        name,
        match okay { true => "yes", false => "no" },
    );
    assert!(okay == !name.contains("unsigned"));
}

Dependencies

~2.5–3.5MB
~62K SLoC