【问题标题】:Regular Expression Python3---why正则表达式 Python3---为什么
【发布时间】:2017-01-10 07:14:49
【问题描述】:

我写了下面的python代码

import re

def get_items():
    text = '''
    <a href="/archive/q-fin">Quantitative Finance</a>
    <a href="/archive/stat">Statistics</a>
    <a href="/help/general">General information</a>
    <a href="/help/support">Support and Governance Model</a>
    <a href="/help/find">Find</a>
    '''
    pattern = re.compile(r'<a href="/archive/(.*?)">(.*?)</a>', re.S)
    items = re.match(pattern, text).group(1)
    print(items)

get_items()

但它不起作用,为什么?

正则表达式如下:

pattern = re.compile(r'<a href="/archive/(.*?)">(.*?)</a>', re.S)

【问题讨论】:

  • 您在某处出错,re.match 可能返回NoneType,因为找不到匹配项。

标签: regex python-3.5


【解决方案1】:

您的正则表达式是正确的,但是您使用了错误的调用来迭代 machtes。请参阅下面使用pattern.finditer(text)match.group(1) 的更正版本。

import re

def get_items():
    text = '''
    <a href="/archive/q-fin">Quantitative Finance</a>
    <a href="/archive/stat">Statistics</a>
    <a href="/help/general">General information</a>
    <a href="/help/support">Support and Governance Model</a>
    <a href="/help/find">Find</a>
    '''
    pattern = re.compile(r'<a href="/archive/(.*?)">(.*?)</a>', re.S)

    for match in pattern.finditer(text):
        yield match.group(1)

for item in get_items():
    print(item)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-28
    • 2011-06-15
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多