Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

How to fix bad escape regex error (python re)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The replacement string for a regex is not a regex in itself, rather it is a string which may contain references to groups captured by the original regex. In your case, you want to capture the year, month and day and then output them in the result string. You do that with () around the values you want to capture, and then refer to the groups by 1, 2, and 3 in the replacement string, with the numbers being assigned in order of the groups being captured. So for your code, you want:

formatted_date = re.sub(r"(d{2,4})-(d{1,2})-(d{1,2})", r"2/3/1", current_date)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...