Python RegEx

PHOTO EMBED

Fri Dec 24 2021 06:49:04 GMT+0000 (Coordinated Universal Time)

Saved by [deleted user]

class RegexMatchTooMuch(Exception): pass


# set up pattern
pattern = re.compile(r'''          
    ^["]*           # text may or may not start with "
    (               # start defining group to return
        [^"]*       # the first character is not "
        \.          # next is .
        csv         # next is csv
    )               # end defining the group to return
    ["]*$           # text may or may not end in "
''', re.VERBOSE+re.IGNORECASE)
reres = pattern.findall('report for RIUD .csv')

if len(reres) == 1:
    print(reres[0])
elif len(reres) == 0:
    print('No match')
else:
    raise RegexMatchTooMuch
content_copyCOPY

https://www.guru99.com/python-regular-expressions-complete-tutorial.html