I would like to create a Unix shell-style pattern that will only match strings with specific suffixes. The pattern in question starts with 0.1 and can finish with test or alpha or . .
This means that 0.1test and 0.1alpha should match but not 0.1beta or 0-1alpha.
I've constructed the following patterns:
0.1{.,test,alpha}0.1{["."],["test"],["alpha"]}
but both fail when tested as such:
import fnmatch
result = fnmatch.fnmatch(v, glob_pattern)
if result:
print('SUCCESS')
else:
print('FAILURE')
The
fnmatch.fnmatchfunction does not support curly braces in the glob pattern.Instead, you can install the
braceexpandmodule to expand a pattern with curly braces into all possible strings that the pattern represents, so that each of them can then be passed tofnmatch.fnmatchfor a match:so that:
outputs:
Demo: https://replit.com/@blhsing1/ColorfulCornsilkGigabyte