It's not hurting you here, but you should get in the habit of using "r-strings" for regex patterns so backslashes don't get modified.
We don't have your filesystem to know what's there. But your pattern seems okay. Your problem may be in the filesystem or how you're trying to prune the names. As mentioned above, use .stem to pull the final component of a Path.
Your pattern is anchored, so it can't match multiple times. Use re.match or re.search instead of re.findall.
We don't have your filesystem to know what's there. But your pattern seems okay. Your problem may be in the filesystem or how you're trying to prune the names. As mentioned above, use .stem to pull the final component of a Path.
Your pattern is anchored, so it can't match multiple times. Use re.match or re.search instead of re.findall.
import re tofind = r'^[A-Z0-9]{8}_\d{4}$' for d in ["ED1234ND_2345", "YD1COP1Z_3456", "mydir"]: if re.findall(tofind,d): print(f"{d} Matched") else: print(f"{d} no match")
Output:ED1234ND_2345 Matched
YD1COP1Z_3456 Matched
mydir no match