0

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.

6
  • You can loop over the filenames returned by os.listdir() and do your own regexp matching. Commented Sep 4, 2023 at 0:03
  • Does \babc(_123)\b? work for you? Commented Sep 4, 2023 at 0:08
  • @Barmar: I agree that works for the simple case of filtering files in a single directory but then it does not generalize if the regular expression applies to directories in the pattern. For example, re_glob('path/abc(_123)?/to/*.txt') Commented Sep 4, 2023 at 0:08
  • @Bohemian: That also works for my example but I am more interested in whether there exists an implementation of re_glob or what the best way to implement it would be. Commented Sep 4, 2023 at 0:11
  • @Barmar. I edited the post to mention that example as I see my example before didn't make clear the kind of cases I was imagining. Commented Sep 4, 2023 at 0:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.