使用正则表达式[重复]检索字符串的最后3个单词

问题描述 投票:0回答:1

我正在尝试使用正则表达式检索字符串的最后3个字。

字符串-“从2019年7月19日出发”

我只需要提取日期部分'2019年7月19日'。

我尝试了以下方式:-

(\w{3})$             # gives last 3 letters of the last word
([,0-9]{3})$         # gives last 3 digits from the last word
(?:\w+\s*){1,3}$     # retrieves only the last word

他们都不给我最后三个字。

regex
1个回答
0
投票

尝试一下:

string = "From the place on July 19, 2019"
mobj = re.search(r"\b(\w+(?=\s\d+,)\s\d+,\s?\d+)", string)
date = mobj.group(1)

输出:

>>> print(date)
July 19, 2019
© www.soinside.com 2019 - 2024. All rights reserved.