-1

I want to make a list of several PNG in a folder based on multiple references. So in the list I want the PNG that have the string "7029113" OR "7031503" in their name. This is what I got so far, I only need to know how to do OR with regex, and probably my wildcards are wrong too I'm not sure.

render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")

r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))  
globglob
  • 143
  • 8
  • `"(".*7029113.*|.*7031503.*")"`. That can probably be shortened by taking the common parts out of the group (`.*70` and `3.*`). – 9769953 Nov 06 '19 at 12:13
  • If you use `re.search` instead of `re.match`, you don't need the `.*` prefix and postfix; just matching on the substring would be enough to yield a True value. – 9769953 Nov 06 '19 at 12:15
  • Do not use `re.match` if you do not want to only search for matches at the start of the string. Use `re.search`. And the pattern is basic: `word1|word2` – Wiktor Stribiżew Nov 06 '19 at 12:57

1 Answers1

1

This is one way of doing it.

r = re.compile('.*(7029113|7031503).*')
nitin3685
  • 825
  • 1
  • 9
  • 20
  • Thank you! Do you mind explaining the ".*" I know it's a wildcard but I don't understand why it's not just the dot. – globglob Nov 06 '19 at 13:10
  • `.` is often used where you want to match “any character”. Please read the following documentation to know more about regex. `https://docs.python.org/3/howto/regex.html#matching-characters` – nitin3685 Nov 07 '19 at 04:51