I'm attempting to dynamically build a regex string for a match check. It will use part of users input, the problem I'm facing is when I build the string (which uses numerical digits \d{4} for example) it ends up with an additional \ backslash, to escape the initial one.
So the re.match never works. I initially thought this was a simple problem to solve, but after lots of googling and failed attempts at Arr what if. I must be missing something, any pointers or insight would be appreciated.
if args['del']:
delimiter = args['del']
else:
delimiter = '/'
if args['start']:
test = "\d{{2}}{0}\d{{2}}{0}\d{{4}}".format(delimiter)
match = re.match(test, args['start'])
if match:
sdate = args['start']
print(sdate)
else:
print('Format: dd{}mm{}yyyy'.format(delimiter))
sys.exit()
rin front of the string to avoid Python trying to interpret them:r"\d{{2}}{0}\d{{2}}{0}\d{{4}}"args['start]with a DOB string such as '01/01/1900' and try it yourself. It's using python 3.6.15.delimiter, if I use a regular/it matches fine usingr"", but when using a different special character such as:it breaks. So I attempt to escape this with a slash, that slash then gets an additional slash to escape it.