【发布时间】:2021-03-17 23:54:33
【问题描述】:
我正在尝试使用正则表达式提取持续时间,
示例文本,
text = "Google, Inc 09/19 - 09/20 CA, USA"
这里是我的正则表达式,
pattern = fr"""
(?:
(
\d\d(?:\.|\/)\d\d\d\d|
(?:{months_abr})?
(?:{months_exp})?
(?:
(?:[\s\.\/\-]?\d{{2,4}})
)
)\s*(?:\-|to|\s)\s*
(
\d\d(?:\.|\/)\d\d\d\d|
(?:{months_abr})?
(?:{months_exp})?
(?:
(?:[\s\.\/\-]?\d{{2,4}})
)|
current|present|till\s?\-?date|till\s?\-?now|till\s?\-?date|to\s\-?present|until\s?\-?now|till\s?\-?now
)
)"""
find_all = re.findall(
pattern, text, flags=re.MULTILINE | re.VERBOSE | re.IGNORECASE
)
我得到的输出,
[('/19', '09')]
【问题讨论】:
-
预期输出是什么?
-
这是我想要的输出,
('09/19', '09/20')。我当前的正则表达式处理其他模式,例如(march 1996, april 2020),但我的正则表达式对于上述文本失败,我不知道为什么。 -
我尝试对模式进行了一些优化,我认为this one 可以是您寻求的正则表达式。
标签: python python-3.x regex re