Is there a currently available way in Python to search for pathnames using regular expressions when globbing is insufficient? For example,
# Match abc_123/ and abc/ but not abcd_123/ or abcd/
re_glob(r'abc(_123)?')
# Match path/abc_123/to/file.txt but not path/abcd/to/file.txt
re_glob('path/abc(_123)?/to/*.txt')
Any example I come up with can be accomplished with globbing followed by regex filtering but I am interested if a simple regex only option is available or straightforward to implement. Looking at similar questions (1, 2, 3) it seems no such option is proposed or, if it is, it is a naive walk through all subdirectories of the pattern's root folder followed by regex filtering.
If no available solutions exist, any significant downside to extending the glob.glob() implementation? It seems like the only change needed is replacing fnmatch.filter() with re.fullmatch() and extending has_magic to has_regex.
os.listdir()and do your own regexp matching.\babc(_123)\b?work for you?re_glob('path/abc(_123)?/to/*.txt')