regex - 使用正则表达式检索字符串的最后 3 个单词
问题描述
我正在尝试使用正则表达式检索字符串的最后 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
他们都没有给我最后三个字。
解决方案
尝试这个:
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
推荐阅读
- c# - 没有装饰器的反序列化
- statistics - 推荐的 Gap 统计模拟参考数据集数量
- excel - 在 Outlook vba 中打开 Excel 工作簿时出现不匹配错误
- swift - 为什么这不打开导航控制器窗口?
- java - 在 Java 9+ 中启动 jstatd
- python - 如何在boto3中获取过滤后的objectsCollection的大小
- jquery - jQuery调整对话框大小移动其边框
- python - 我可以使用 Django 模型对数据库执行复杂的查询吗?
- sql - MS Access Select 有效,但更新无效
- java - 使用元素 BufferedReader 和 BufferedWriter 创建数组对象