1

This is an easy question, but I am still getting stuck. I have a bunch of files and I have to check whether they are following this format.

abc_monthname[0-9]_v[0-9].xlsx

I have done some very generic like : r^[A-Za-z]+_[A-Za-z0-9]+_v[0-9]+\.xlsx$'' but this will fail for some cases. I want to give a strict rule. How do I achieve this in python?

1
  • 2
    For future reference, please avoid changing your question after answers have already been given. Commented Oct 17, 2022 at 1:46

1 Answer 1

2

You probably want to use + quantifiers with the numeric portion of your regex:

^abc_monthname[0-9]+_v[0-9]+\.xlsx$

Note also that dot is a metacharacter and should be escaped with backslash. Here is a sample script

filename = "^abc_monthname123_v2.xslx"
if re.search(r'^abc_monthname[0-9]+_v[0-9]+\.xlsx$', filename):
    print("MATCH")
Sign up to request clarification or add additional context in comments.

2 Comments

Something like :^[A-Za-z]+_[A-Za-z0-9]+_v[0-9]+\.xlsx$ Is this right?
The answer to your question totally depends on what you are trying to match.

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.