[英]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 Ymd to M/d/y.我一直在搞乱re.sub()
以了解如何将格式从 Ymd 更改为 M/d/y。 To perform the test, I defined the starting variable: current_date = "2012-05-26"
为了执行测试,我定义了起始变量: current_date = "2012-05-26"
I would try to achieve to convert that date to 05/26/2012.我会尝试将该日期转换为 2012 年 5 月 26 日。
I tried to achieve this without using DateTime but with regex.我试图在不使用 DateTime 而是使用正则表达式的情况下实现这一点。 I used re.sub
as below:我使用re.sub
如下:
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 YMD and the second Regex is to try to convert it to the format that I want it to be.第一个正则表达式是匹配 YMD 的原始格式,第二个正则表达式是尝试将其转换为我想要的格式。 I got the following error:我收到以下错误:
Traceback (most recent call last):
File "C:\Users\ghub4\AppData\Local\Programs\Python\Python39\lib\sre_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:\Users\ghub4\OneDrive\Desktop\test_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:\Users\ghub4\AppData\Local\Programs\Python\Python39\lib\re.py", line 210, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "C:\Users\ghub4\AppData\Local\Programs\Python\Python39\lib\re.py", line 327, in _subx
template = _compile_repl(template, pattern)
File "C:\Users\ghub4\AppData\Local\Programs\Python\Python39\lib\re.py", line 318, in _compile_repl
return sre_parse.parse_template(repl, pattern)
File "C:\Users\ghub4\AppData\Local\Programs\Python\Python39\lib\sre_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.我已经将错误追踪到潜在的第二个正则表达式,但我不确定 position 9 在哪里以及如何修复错误。 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
.我不确定如何修复它的另一个原因是由于第一个错误指出\\d
引发的 keyerror 。 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.我敢肯定,当正则表达式在代码中的某处解释时,它会将\d
作为\\d
而不是我也不确定如何防止这种情况。 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?我将如何纠正这些错误?
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.在您的情况下,您希望在结果字符串中捕获年、月和日,然后是 output 它们。 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.您可以在要捕获的值周围使用()
执行此操作,然后在替换字符串中通过\1
、 \2
和\3
引用组,并按照捕获组的顺序分配数字。 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)
Try and group your digits (If you goal is testing then position 9 is your first \d in your second regex-check - It is an invalid group reference):尝试对您的数字进行分组(如果您的目标是测试,那么 position 9 是您的第二个正则表达式检查中的第一个 \d - 这是一个无效的组参考):
formatted_date = re.sub(r"(\d{2,4})-(\d{1,2})-(\d{1,2})",r"\2/\3/\1",current_date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.