patternMatch

Matches a pattern against a filename.

Some characters of pattern have special a meaning (they are meta-characters) and can't be escaped. These are:

*Matches 0 or more instances of any character.
?Matches exactly one instances of any character.
[chars]Matches one instance of any character that appears between the brackets.
[!chars]Matches one instance of any character that does not appear between the brackets after the exclamation mark.

Internally individual character comparisons are done calling charMatch(), so its rules apply here too. Note that path separators and dots don't stop a meta-character from matching further portions of the filename.

bool
patternMatch
(
cstring filename
,
cstring pattern
)

Return Value

Type: bool

true if pattern matches filename, false otherwise.

Throws

Nothing.

patternMatch("Go*.bar", "[fg]???bar"); // => false
patternMatch("/foo*home/bar", "?foo*bar"); // => true
patternMatch("foobar", "foo?bar"); // => true

Examples

test(!patternMatch("foo", "Foo"));

test(patternMatch("foo", "*"));
test(patternMatch("foo.bar", "*"));
test(patternMatch("foo.bar", "*.*"));
test(patternMatch("foo.bar", "foo*"));
test(patternMatch("foo.bar", "f*bar"));
test(patternMatch("foo.bar", "f*b*r"));
test(patternMatch("foo.bar", "f???bar"));
test(patternMatch("foo.bar", "[fg]???bar"));
test(patternMatch("foo.bar", "[!gh]*bar"));

test(!patternMatch("foo", "bar"));
test(!patternMatch("foo", "*.*"));
test(!patternMatch("foo.bar", "f*baz"));
test(!patternMatch("foo.bar", "f*b*x"));
test(!patternMatch("foo.bar", "[gh]???bar"));
test(!patternMatch("foo.bar", "[!fg]*bar"));
test(!patternMatch("foo.bar", "[fg]???baz"));

Meta