0

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()
9
  • Since you're wanting to use backslashes in the regex string, just put an r in front of the string to avoid Python trying to interpret them: r"\d{{2}}{0}\d{{2}}{0}\d{{4}}" Commented Dec 12, 2021 at 23:39
  • Thanks for the suggestion. I have tried that, it still escapes the initial `\` Commented Dec 13, 2021 at 8:17
  • You say it "escapes it" but from your question and comment it's not clear what actually happens. I'm positive you're wrong about Python somehow still using the first backslash as an escape character. From your question it would appear you're confusing a printed representation for the value of the string - but it's impossible to say if you don't provide an example that actually runs and shows what you think the problem is. Commented Dec 13, 2021 at 11:16
  • Personally the question seems pretty clear, im trying to build a regex string to match a DOB string. When the Regex is formed its being escaped with double slashes, so the MATCH function never evaluates as true. What more would you like me to say? The code is there for you to try. Feel free to replace the args['start] with a DOB string such as '01/01/1900' and try it yourself. It's using python 3.6.15. Commented Dec 13, 2021 at 16:16
  • Ok, it seems the problem lies with the delimiter, if I use a regular / it matches fine using r"", 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. Commented Dec 13, 2021 at 16:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.