I am trying to match a list of file names using a regex. Instead of matching just the full name, it is matching both the name and a substring of the name.
Three example files are
t0 = r"1997_06_daily.txt"
t1 = r"2010_12_monthly.txt"
t2 = r"2018_01_daily_images.txt"
I am using the regex d.
a = r"[0-9]{4}"
b = r"_[0-9]{2}_"
c = r"(daily|daily_images|monthly)"
d = r"(" + a + b + c + r".txt)"
when I run
t0 = r"1997_06_daily.txt"
t1 = r"2010_12_monthly.txt"
t2 = r"2018_01_daily_images.txt"
a = r"[0-9]{4}"
b = r"_[0-9]{2}_"
c = r"(daily|daily_images|monthly)"
d = r"(" + a + b + c + r".txt)"
for t in (t0, t1, t2):
m = re.match(d, t)
if m is not None:
print(t, m.groups(), sep="\n", end="\n\n")
I get
1997_06_daily.txt
("1997_06_daily.txt", "daily")
2010_12_monthly.txt
("2010_12_monthly.txt", "monthly")
2018_01_daily_images.txt
("2018_01_daily_images.txt", "daily_images")
How can I force the regex to only return the version that includes the full file name and not the substring?