![](/img/trans.png)
[英]Python regex: get all the non digits, in groups, followed or not by a space
[英]Get links with one hypen followed by 4 digits in regex
我的意图是提取类似http://www.realclearpolitics.com/epolls/2010/governor/ks/kansas_governor_brownback_vs_holland-1235.html
链接
链接应以连字符,4位数字和html扩展名结尾
这是我的源代码
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"http://www\.realclearpolitics\.com/epolls/\d{4}/governor/.+?\-\d{4}\.html"
test_str = "value=\"http://www.realclearpolitics.com/epolls/2010/house/wv/west_virginia_3rd_district_maynard_vs_rahall-1385.html\">West Virginia 3rd District</option> </select></p></div></div><div id=\"leftbox-latest\"><span class=\"sidebar-header\">2010 Governor Races</span><div id=\"poll_sidebar\"><p><strong><img src=\"http://assets.realclearpolitics.com/images/arrow_black.gif\" border=\"0\" alt=\"\" width=\"7\" height=\"10\" /><a href=\"http://www.realclearpolitics.com/epolls/2010/governor/2010_elections_governor_map.html\">RealClearPolitics Ratings</a></strong><br /> <strong><img src=\"http://assets.realclearpolitics.com/images/arrow_black.gif\" border=\"0\" alt=\"\" width=\"7\" height=\"10\" /><a href=\"http://www.realclearpolitics.com/epolls/2010/governor/2010_elections_governor_map_race_changes.html\">RCP Race-by-Race Changes</a></strong><br /> <strong><img src=\"http://assets.realclearpolitics.com/images/arrow_black.gif\" border=\"0\" alt=\"\" width=\"7\" height=\"10\" /><a href=\"http://www.realclearpolitics.com/epolls/2010/governor/2010_elections_governor_map_final_results.html\">Final Governors Map</a></strong><br /> <select class=\"search_by_race\" name=\"search_by_race\"> <option value=\"#\">Governor Races</option> <option value=\"http://www.realclearpolitics.com/epolls/2010/governor/al/alabama_governor_bentley_vs_sparks-1586.html\">Alabama Governor</option> <option value=\"http://www.realclearpolitics.com/epolls/2010/governor/ak/alaska_governor_parnell_vs_berkowitz-1510.html\">Alaska Governor</option> <option value=\"http://www.realclearpolitics.com/epolls/2010/governor/az/arizona_governor_brewer_vs_goddard-1409.html\">Arizona Governor</option> <option value=\"http://www.realclearpolitics.com/epolls/2010/governor/ar/arkansas_governor_keet_vs_beebe-1568.html\">Arkansas Governor</option> <option value=\"http://www.realclearpolitics.com/epolls/2010/governor/ca/california_governor_whitman_vs_brown-1113.html\">California Governor</option> <option value=\"http://www.realclearpolitics.com/epolls/2010/governor/co/colorado_governor_maes_vs_hickenlooper_vs_tancredo-1677.html\">Colorado Governor</option> <option "
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
第一场比赛是错误的。 我不知道如何排除第一个,而包括其余的。 我相信正则表达式正在对连字符进行贪婪的搜索。 如果这是真的,该如何避免呢?
在您的正则表达式中,您使用.
可以匹配任何字符。 您需要使用[^"\\s]
来限制该部分,以匹配除"
或空格之外的任何字符。
我建议你用
regex = r'http://www\.realclearpolitics\.com/epolls/\d{4}/governor/[^\s"]+-\d{4}\.html'
参见Python演示
细节
http://www\\.realclearpolitics\\.com/epolls/
-文字http://www.realclearpolitics.com/epolls/
子字符串 \\d{4}
-4位数字 /governor/
-文字子字符串 [^\\s"]+
-除空格和"
-
连字符 \\d{4}
-4位数字 \\.html
- .html
子字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.