0

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?

3 Answers 3

1

You should make your c pattern non-capturing with '?:'

c = r"(?:daily|daily_images|monthly)"
Sign up to request clarification or add additional context in comments.

Comments

0

This is working correctly. The issue you are seeing is how groups work in regex. Your regex c is in parentheses. Parentheses in regex signify that this match should be treated as a group. By printing m.group(), you are printing a tuple of all the groups that matched. Luckily, the first element in the group is always the full match, so just use the following:

print(t, m.groups()[0], sep="\n", end="\n\n")

Comments

0

I know you're only looking for regex solutions but you could easily use os module to split the extension and return index 0. Otherwise, as Bill S. stated, m.groups()[0] returns the 0th index of the regex group.

# os solution
import os

s = "1997_06_daily.txt"

os.path.splitext(s)[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.