I've been messing around with re.sub()
to see how I would change the format from Y-m-d to M/d/y. To perform the test, I defined the starting variable: current_date = "2012-05-26"
I would try to achieve to convert that date to 05/26/2012.
I tried to achieve this without using DateTime but with regex. I used re.sub
as below:
formatted_date = re.sub(r"d{2,4}-d{1,2}-d{1,2}", r"[^a-zA-Z]d{1,2}/d{1,2}/d{2,4}", current_date)
The first regex is to match the original format of Y-M-D and the second Regex is to try to convert it to the format that I want it to be. I got the following error:
Traceback (most recent call last):
File "C:Usersghub4AppDataLocalProgramsPythonPython39libsre_parse.py", line 1039, in parse_template
this = chr(ESCAPES[this][1])
KeyError: '\d'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:Usersghub4OneDriveDesktopest_sub.py", line 5, in <module>
formatted_date = re.sub(r"d{2,4}-d{1,2}-d{1,2}", r"[^a-zA-Z]d{1,2}/d{1,2}/d{2,4}", current_date)
File "C:Usersghub4AppDataLocalProgramsPythonPython39lib
e.py", line 210, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "C:Usersghub4AppDataLocalProgramsPythonPython39lib
e.py", line 327, in _subx
template = _compile_repl(template, pattern)
File "C:Usersghub4AppDataLocalProgramsPythonPython39lib
e.py", line 318, in _compile_repl
return sre_parse.parse_template(repl, pattern)
File "C:Usersghub4AppDataLocalProgramsPythonPython39libsre_parse.py", line 1042, in parse_template
raise s.error('bad escape %s' % this, len(this))
re.error: bad escape d at position 9
Full Code:
import re
current_date = "2012-05-26"
formatted_date = re.sub(r"d{2,4}-d{1,2}-d{1,2}", r"[^a-zA-Z]d{1,2}/d{1,2}/d{2,4}", current_date)
print(formatted_date)
I've traced the error to potential the second regex but I'm unsure where position 9 is and how to fix the error. Another reason why I'm not sure how to fix it is due to the first error where it stated a keyerror raised by \d
. I'm sure that when the regex is interpret somewhere in the code, it is taking the d
as \d
instead which Im also not sure how to prevent that. I'm also pretty sure that the second regex may backfire on me and I am working on a solution on that after this question is posted. How would I be able to correct these errors?
question from:
https://stackoverflow.com/questions/66057764/how-to-fix-bad-escape-regex-error-python-re