我想从大量文件中查找日期。日期在一行上,格式为"21 September 2010"
。每个文件中只有一个这样的日期。
以下代码仅返回月份,例如,"September"
。为什么group(0)不给我像"21 September 2010"
这样的全部内容?
这里缺少什么?谢谢!
months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
pattern = r"^\d{2} +" + "|".join(months) + r" +\d{4}$"
match = re.search(pattern, text)
if match:
fdate = match.group(0)
最佳答案
当您打印正则表达式时,您会看到它看起来像^\d{2} +January|February|March|April|May|June|July|August|September|October|November|December +\d{4}$
。将其应用于21 September 2010
时,您将see that it matches September
,因为^\d{2} +
只能与字符串开头的January
匹配,因为未对月份替代项进行分组。
您需要对月份替代方案进行分组:
pattern = r"^\d{{2}} +(?:{}) +\d{{4}}$".format("|".join(months))
请参见Python demo:
import re
text = "21 September 2010"
months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
pattern = r"^\d{{2}} +(?:{}) +\d{{4}}$".format("|".join(months))
match = re.search(pattern, text)
if match:
fdate = match.group(0)
print(fdate) # => 21 September 2010