9

glob.glob() does not use regex. it uses Unix path expansion rules. How can I emulate this regex in glob:

".*.jpg|.*.png"
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
alzoubi36
  • 145
  • 1
  • 5
  • 1
    ```.*.{jpg,png}``` would be valid in a shell, but glob doesn't support that. But see this question for a workaround: [Brace expansion in python glob](https://stackoverflow.com/questions/22996645/brace-expansion-in-python-glob) – sj95126 Sep 23 '21 at 14:50

2 Answers2

10

Well, with glob you should just do this:

lst = glob.glob('*.jpg') + glob.glob('*.png')
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • thanks for the response. I am actually looking for a more elegant way. This will look like this if I use the pathlib module since its glob() method returns a generator object: lst = list(pathlib.Path.glob('*.jpg')) + list(pathlib.Path.glob('*.png')) – alzoubi36 Sep 23 '21 at 12:17
  • @alzoubi36 Yeah, that's the way, there isn't any more elegent solution unfortunately, feel free to mark and vote this if you like :) – U13-Forward Sep 23 '21 at 12:18
  • I voted up for the effort, but I won't mark it since I knew this solution. @U12-Forward – alzoubi36 Sep 23 '21 at 12:22
  • @alzoubi36 Well, this is the *only* one :) – U13-Forward Sep 23 '21 at 12:23
4

@U12-Forward is correct that there isn't an exact solution but depending on your use case you might be able to solve it with the [...] wildcard. For your example with .png or .jpg you could use this:

.*.[jp]*

which will match any extension that starts with a j or p

If you have other extensions that start with j or p you can be more specific:

.*.[jp][pn]g
baconcheese113
  • 843
  • 2
  • 13
  • 27