I am trying to match file names in the format filename-isodate.txt
>>> DATE_NAME_PATTERN = re.compile("((.*)(-[0-9]{8})?)\\.txt")
>>> DATE_NAME_PATTERN.match("myfile-20101019.txt").groups()
('myfile-20101019', 'myfile-20101019', None)
However I need to get the filename and -isodate parts in seperate groups.
Any suggestions and/or explainations would be much appreciated
You need:
DATE_NAME_PATTERN = re.compile("((.*?)(-[0-9]{8})?)\\.txt")
.* performs a gready match so the second part is never used.
FYI in my opiniomy you shouldn’t use regular expression where normal string manipulation is enough ( simple split() will do ).