【发布时间】:2023-03-28 17:05:01
【问题描述】:
我们如何编写正则表达式来提取文本中的年份,年份可能有以下形式
Case 1:
1970 - 1980 --> 1970, 1980
January 1920 - Feb 1930 --> 1920, 1930
May 1920 to September 1930 --> 1920, 1930
Case 2:
July 1945 --> 1945
为Case 1 编写正则表达式很容易,但我如何处理Case 2 以及它
\d{4} \s? (?: [^a-zA-Z0-9] | to) \s? \w+? \d{4}
【问题讨论】:
-
你能用
\b\d{4}\b吗? -
Writing regular expression for Case 1 is easy我会支持一下。您的正则表达式不匹配任何内容。
标签: regex python-3.x information-extraction