iNoob
iNoob

Reputation: 1395

Building a dynamic regex string in Python3.x

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()

Upvotes: 0

Views: 67

Answers (1)

iNoob
iNoob

Reputation: 1395

OK, so this was a simple fix, when defining my args as inputs. I was setting a custom delimiter with --del but not using that same delimiter in my DOB strings.

So obviously it was not matching (facepalm). @Grismar's suggestion that I wasn't understanding the problem correctly, made me go over it again (with fresh eyes) and I worked it out. Thanks

Upvotes: 1

Related Questions