<jupyter_start><jupyter_text>[fnmatch](https://docs.python.org/3/library/fnmatch.html)1. What is fnmatch and why is it useful?1. Why should I use fnmatch and not regex?1. Two examplesFnmatch is part of the python standard library. Allows the use of UNIX style wildcards for string matching. Makes it easy to select a single file type out of a list (e.g. *.csv).While regex is much more powerful, fnmatch offers a simple syntax for using wildcards.If you want to look for a string that starts with 5 characters, then a space and then 3 numbers between 4 and 7 you'll still need to resort to regex though.  Simple example<jupyter_code>import fnmatch

FILES = ["some_picture.png", "some_data.csv", "another_picture.png"]

# select only the .png files
for file in FILES:
    if fnmatch.fnmatch(file, '*.png'):
        print(file)
        
# or using the fnmatch shorthand
print(fnmatch.filter(FILES, '*.png'))<jupyter_output>some_picture.png
another_picture.png
['some_picture.png', 'another_picture.png']<jupyter_text>*SIDE NOTE*: The matching is **case insensitive**, if you want to perform a case sensitive match use [`fnmatch.fnmatchcase()`](https://docs.python.org/3/library/fnmatch.htmlfnmatch.fnmatchcase) Match a list of patterns<jupyter_code>MODELS = ["MESSAGEix-GLOBIOM 1.0",
          "MESSAGEix-GLOBIOM 1.1",
          "REMIND-MAgPIE 2.1-4.2",
          "REMIND-MAgPIE 1.7-3.2",
          "NIGEM",
          "POLES GECO2019",
          "COFFEE 1.0",
          "COFFEE 2.0",
          "TEA",
          "GCAM5.2",
          "GCAM5.3"]

MATCH_MODELS = ["MESSAGEix-GLOBIOM*", "REMIND-MAgPIE*"]

match_any = lambda x, patterns: any(fnmatch.fnmatch(x, pattern) for pattern in patterns)

for m in MODELS:
    if match_any(m, MATCH_MODELS):
        print(m)<jupyter_output>MESSAGEix-GLOBIOM 1.0
MESSAGEix-GLOBIOM 1.1
REMIND-MAgPIE 2.1-4.2
REMIND-MAgPIE 1.7-3.2
{'fraction_non_alphanumeric': 0.11505376344086021, 'fraction_numerical': 0.024193548387096774, 'mean_word_length': 3.5390243902439025, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '667993', 'license': 'MIT', 'n_tokens_mistral': 677, 'n_tokens_neox': 640, 'n_words': 193, 'path': '2021-06-09-fnmatch.ipynb', 'repo_name': 'phackstock/code-and-tell', 'size': '1860'}