#195 · Strings · Expert · 260 XP · about 50 min
Tools such as .gitignore files and build scripts pick out files with glob patterns. Write glob_match(pattern, path) that returns True if the whole path matches the whole pattern, and False otherwise. Paths use / between folder names, and these pattern pieces are special:
? matches exactly one character that is not /.* matches any run of characters (including none) that contains no /, so it never leaves its folder.**/ matches any number of whole folders, including none: nothing, a/, a/b/ and so on.** anywhere else matches any run of characters, / included.[abc] matches one of the listed characters, [a-z] one character in that range, and [!abc] or [!a-z] one character that is not listed. A bracket never matches /, and a - at the start or end of a bracket is an ordinary character.glob_match("*.py", "main.py") # returns True glob_match("*.py", "src/main.py") # returns False glob_match("src/**/*.py", "src/main.py") # returns True glob_match("report_[0-9][0-9].csv", "report_07.csv") # returns True
In the third example **/ matches no folders at all, and * matches main.
pattern has 0 to 100 characters and path has 0 to 200 characters.[ has a matching ] with at least one character in between, brackets do not contain / or ], and there are never three * in a row./ and never contain //.Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.