I need to iterate over files and select those that end with _FirstPerson_04312.json or _Hybrid_04312.json, where 04312 can be any sequence of digits 0-9.
I haven’t used regex much so far, can someone help me how to do this?
Thanks!
my code so far:
for fname in os.listdir(path=search_path):
if fname.endswith(".json"):
# if fname contains -- regex
>Solution :
You can match both json ending and number pattern in one go with the re package:
import re
for fname in os.listdir(path=search_path):
if re.match(".*?_(?:FirstPerson|Hybrid)_\d+.json", fname):
# ...