Module fnmatch_regex::glob
source · [−]Expand description
Shell glob-like filename matching.
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\vsequences - 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.txtwould only matchabc.txtfoo/test?.txtwould match e.g.foo/test1.txtorfoo/test".txt, but notfoo/test/.txt/etc/c[--9].confwould match e.g./etc/c-.conf,/etc/c..conf, or/etc/7.conf, but not/etc/c/.conflinux-[0-9]*-{generic,aws}would matchlinux-5.2.27b1-genericandlinux-4.0.12-aws, but notlinux-unsigned-5.2.27b1-generic
Note that the glob_to_regex function returns a regular expression
that will only verify whether a specified text string matches
the pattern; it does not in any way attempt to look up any paths on
the filesystem.
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"));
}Functions
Parse a shell glob-like pattern into a regular expression.