0

I want to check whether file names in a directory correspond to a regex pattern, that works. Now it would be nice to actually mark the error, so the user knows what to fix. Is there a way to do this with re directly (preferable using match() or fullmatch()) so that I don't have to write ugly code with 20 different conditions?

This is my code.

def checkname():
    dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",".."))
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if re.match(r".+\.(mei|mscz|mxl)$",file) and not re.match(r"PL_WRk_352_\d+(r|v)(-\d+(r|v))?_[^\W\d]+_n\d+_enc_dipl_CMN",file):
                print(f"wrong file: {os.path.join(root, file)}")

It correctly prints out all the wrong files buit this is not too useful since the filenames are complex and a lot of mistakes can happen. So it would be great to now where re.match(r"PL_WRk_352_\d+(r|v)(-\d+(r|v))?_[^\W\d]+_n\d+_enc_dipl_CMN",file) failed to show the user at least the first error. Is this possible?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
brathenne
  • 29
  • 5
  • 1. How does the program know that a file should have matched in order to indicate a problem? 2. It sounds like you're trying to re-implement regex101.com for your users? – Harvey Apr 25 '23 at 19:24
  • For this particular regex there is a solution. Too bad the question was closed. – trincot Apr 25 '23 at 20:10
  • I had to use some ugly workaround. Basically I have a list of strings where the elements are the regex building blocks `li = ['n', '\d+', '_']`. Then I just remove the last element of the list in a loop, turn it into a regex and apply it to the file-string. When I get a match, I'll output it. This works because the regex gets shorter and shorter until it doesn't clash with the file no longer – brathenne Apr 29 '23 at 09:54

0 Answers0