Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在正则表达式中获取一个后跟4位数字的hypen链接_Python_Regex_Python 2.7 - Fatal编程技术网

Python 在正则表达式中获取一个后跟4位数字的hypen链接

Python 在正则表达式中获取一个后跟4位数字的hypen链接,python,regex,python-2.7,Python,Regex,Python 2.7,我的目的是提取像http://www.realclearpolitics.com/epolls/2010/governor/ks/kansas_governor_brownback_vs_holland-1235.html 链接应该以连字符、4digits和html扩展名结尾 这是我的源代码 # coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility i

我的目的是提取像
http://www.realclearpolitics.com/epolls/2010/governor/ks/kansas_governor_brownback_vs_holland-1235.html

链接应该以连字符、4digits和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.
#编码=utf8
#上面的标记定义了此文档的编码,并且用于Python 2.x兼容性
进口稀土
regex=r“http://www\.realclearpolitics\.com/epolls/\d{4}/governor/+?\-\d{4}\.html“

测试\u str=“值=\”http://www.realclearpolitics.com/epolls/2010/house/wv/west_virginia_3rd_district_maynard_vs_rahall-1385.html\“>西弗吉尼亚州第三区2010年州长竞选



<阿拉巴马州州长阿拉巴马州州长阿拉斯加州州长亚利桑那州州长阿肯色州州长加利福尼亚州州长科罗拉多州州长在正则表达式中,您使用了可以匹配任何字符的
。您需要使用
[^”\s]
限制该部分,以匹配除
或空白以外的任何字符

我建议你用

regex = r'http://www\.realclearpolitics\.com/epolls/\d{4}/governor/[^\s"]+-\d{4}\.html'

详细信息

  • http://www\.realclearpolitics\.com/epolls/
    -文字
    http://www.realclearpolitics.com/epolls/
    子字符串
  • \d{4}
    -4位数字
  • /governor/
    -文字子字符串
  • [^\s”]+
    -1+个字符,除了空格和
  • -
    -连字符
  • \d{4}
    -4位数字
  • \.html
    -a
    .html
    子字符串

能否包含Python代码?另外,只需向我们展示一个小样本文本,而不是一个巨大的HTML转储文件;这里很少有人有耐心通读它。你的代码,至少是一个较短的输入测试字符串。当我逐字使用您的代码时,我得到的是关于输入字符串的错误,而不是正则表达式逻辑的错误,这在我看来很好。@virupaksha在您的编辑中,您似乎丢失了实际的问题。第一个匹配错误。这就是问题所在。其他人都对了,我看我被否决了。有人能告诉我为什么吗?