[英]How to match to capture group 1 with regex
My goal is to capture the date from the following string:我的目标是从以下字符串中捕获日期:
<span class="ui_bubble_rating bubble_50"></span><span class="ratingDate relativeDate" title="November 9, 2017">Reviewed 2 days ago </span><a class="viaMobile" href="/apps" target="_blank" onclick="ta.util.cookie.setPIDCookie(24487)"><span class="ui_icon mobile-phone"></span>via mobile </a>
To do this I'm using the regex: title="(*?)"
Which returns Match (group 0): title="November 9, 2017"
Group 1: November 9, 2017
为此,我使用了正则表达式:
title="(*?)"
返回 Match (group 0): title="November 9, 2017"
Group 1: November 9, 2017
I need my match returned by regex to be just the date, what is currently group 1. Is there a simple way to do this?我需要正则表达式返回的匹配只是日期,当前是第 1 组。有没有简单的方法可以做到这一点? I am new to regex but I could find direction on this online.
我是正则表达式的新手,但我可以在网上找到方向。
Note: I'm not writing regex for the structure of a date because some strings have multiple dates and I only want the date in title.注意:我没有为日期结构编写正则表达式,因为某些字符串有多个日期,我只想要标题中的日期。 Thanks!
谢谢!
You can use negative lookahead / lookbehind instead of capture groups您可以使用负前瞻/后视而不是捕获组
(?<=title=\").+?(?=")
This will ensure it starts with title, without actually selecting it这将确保它以标题开头,而不是实际选择它
You can use re.findall
:您可以使用
re.findall
:
import re
s = """
<span class="ui_bubble_rating bubble_50"></span><span class="ratingDate relativeDate" title="November 9, 2017">Reviewed 2 days ago </span><a class="viaMobile" href="/apps" target="_blank" onclick="ta.util.cookie.setPIDCookie(24487)"><span class="ui_icon mobile-phone"></span>via mobile </a>
"""
date = re.findall('title="(.*?)"', s)[0]
Output: Output:
'November 9, 2017'
You can also use你也可以使用
title="\K.*?(?=")
This will look for the value between title="
and "
这将查找
title="
和"
之间的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.