【发布时间】:2019-10-16 19:36:21
【问题描述】:
我对python有点陌生,对于这个作业,我们被要求使用一个正则表达式来解决每个提示。我已经完成了提示 A-C,但现在我卡在了提示 D。 提示如下:
d。使用正则表达式的替换,将“2019 年 5 月 29 日”或“2019 年 5 月 29 日”格式的日期转换为“19 年 5 月 29 日”。
要匹配的有效日期格式包含以下元素:
•月份必须是常见的三字母月份缩写,以大写字母开头,后跟两个小写字母:Jan、Feb、Mar、Apr、May、Jun、Jul、Aug、Sep、Oct、Nov、Dec.
•日期可能是一位或两位数。不必检查有效日期,可以接受带有前导零的日期,例如 03。
•年份正好是四位数。
• 月份和日期由一个或多个空格分隔。日期和年份也由一个或多个空格分隔,但允许紧跟在日期之后的可选逗号(日期和逗号之间不允许有空格)
我坚持的是:我不确定在 r"..." 语句中放什么(参考代码),现在我得到一个错误 "re.error: bad escape \ w 在位置 0",如果我们可以修复错误或找到另一种方法来做到这一点,同时保持 substr = r"..." 我真的很感激!谢谢!
注意: --我的 re.compile 代码工作得很好,在我弄乱子字符串来更改输出之前,它接受了这种情况。它只是没有转换它,因为我还没有编写转换字符串。 --目前我处理日期的方式不是很传统,我计划在得到一些有用的东西之后再做这件事。
代码:
import re
d = re.compile(r"^((Jan)\s+[1-31],\s+\d{4})$|"
r"^((Jan)\s+[1-31]\s+\d{4})$|"
r"^((Feb)\s+[1-28],\s+\d{4})$|"
r"^((Feb)\s+[1-28]\s+\d{4})$|"
r"^((Feb)\s+[1-29],\s+\d{4})$|" #ask prof about leap years
r"^((Feb)\s+[1-29]\s+\d{4})$|" #ask prof about leap years
r"^((Mar)\s+[1-31],\s+\d{4})$|"
r"^((Mar)\s+[1-31]\s+\d{4})$|"
r"^((Apr)\s+[1-30],\s+\d{4})$|"
r"^((Apr)\s+[1-30]\s+\d{4})$|"
r"^((May)\s+[1-31],\s+\d{4})$|"
r"^((May)\s+[1-31]\s+\d{4})$|"
r"^((Jun)\s+[1-30],\s+\d{4})$|"
r"^((Jun)\s+[1-30]\s+\d{4})$|"
r"^((Jul)\s+[1-31],\s+\d{4})$|"
r"^((Jul)\s+[1-31]\s+\d{4})$|"
r"^((Aug)\s+[1-31],\s+\d{4})$|"
r"^((Aug)\s+[1-31]\s+\d{4})$|"
r"^((Sep)\s+[1-30],\s+\d{4})$|"
r"^((Sep)\s+[1-30]\s+\d{4})$|"
r"^((Oct)\s+[1-31],\s+\d{4})$|"
r"^((Oct)\s+[1-31]\s+\d{4})$|"
r"^((Nov)\s+[1-30],\s+\d{4})$|"
r"^((Nov)\s+[1-30]\s+\d{4})$|"
r"^((Dec)\s+[1-31],\s+\d{4})$|"
r"^((Dec)\s+[1-31]\s+\d{4})$")
subStr = r"\w\s\d{1,2}\s\d{4}"
print("----Part d tests that match (and should change):")
print(d.sub(subStr, "May 29, 2019"))
print("----Part d tests that match (and should remain unchanged):")
print(d.sub(subStr, "May 29 19"))
预期输出:
----Part d tests that match (and should change):
May 29 19
----Part d tests that match (and should remain unchanged):
May 29 19
实际输出(如果我将子字符串留空,以及目前的情况):
Blank:
----Part d tests that match (and should change):
May 29, 2019
----Part d tests that match (and should remain unchanged):
May 29 19
--------------------------------
Current:
----Part d tests that match (and should change):
this = chr(ESCAPES[this][1])
KeyError: '\\w'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Xavier/PycharmProjects/hw7/hw7.py", line 101, in <module>
print(d.sub(subStr, "May 29, 2019"))
File "C:\Users\Xavier\AppData\Local\Programs\Python\Python37\lib\re.py", line 309, in _subx
template = _compile_repl(template, pattern)
File "C:\Users\Xavier\AppData\Local\Programs\Python\Python37\lib\re.py", line 300, in _compile_repl
return sre_parse.parse_template(repl, pattern)
File "C:\Users\Xavier\AppData\Local\Programs\Python\Python37\lib\sre_parse.py", line 1024, in parse_template
raise s.error('bad escape %s' % this, len(this))
re.error: bad escape \w at position 0
【问题讨论】:
-
这些部分
[1-31]不能这样工作,它是一个 character class 匹配123的范围从 1 到 3 和另一个 1 已经被 1 到 3 部分覆盖.您可能会查看 this page 以匹配日期格式。
标签: python regex string char digits