简体   繁体   English

如何使用正则表达式匹配捕获组 1

[英]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.

相关问题 正则表达式 - 在捕获组之前匹配模式(来自捕获) - Regex - match a pattern (from capture) before a capture group 如何匹配正则表达式捕获组之后的所有文本,但只匹配下一个重复匹配? - How do I match all text after a regex capture group but only up to the next repeated match? 如果后面跟着另一个组,如何不捕获正则表达式中的组 - How to not capture a group in regex if it is followed by an another group 如何捕获正则表达式匹配项和正则表达式匹配项上方的行并将其发送到文件? - How to capture regex match & line above regex match and send it to a file? 正则表达式仅当捕获组出现在字符串的最后一个时才匹配 - Regex match only when capture group occurs last in string 为什么在第一个捕获组再次出现之前,此正则表达式不匹配所有内容? - Why does this regex not match everything till recurrence of first capture group? Python如何替换正则表达式捕获组中的内容? - Python how to replace content in the capture group of regex? 如何在正则表达式或中选择正确的捕获组? - How to select correct capture group within regex OR? 如何使用正则表达式捕获正确的重复组? - How to use regex to capture the correct repeated group? 正则表达式 - 如何在一组中捕获两种模式? - regex - how to capture two patterns in one group?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM