【问题标题】:How to search for question mark and / with regular expression ? python如何使用正则表达式搜索问号和/? Python
【发布时间】:2015-01-23 20:24:52
【问题描述】:

我想在文件中搜索与此模式匹配的数字:

<a  href="test/?n=451484"   >

然后获取号码451484

我使用这种模式:

'
(test/?n=)
\d+
'

但这不起作用?

【问题讨论】:

    标签: python html regex python-2.7 html-parsing


    【解决方案1】:

    或者,您可以使用专门的工具:

    例子:

    import re
    from urlparse import urlparse, parse_qs
    from bs4 import BeautifulSoup
    
    data = """
    <div>
        <a href="test/?n=451484">link</a>
    </div>
    """
    
    soup = BeautifulSoup(data)
    
    # filtering links with a specific "href" attribute value    
    link = soup.find('a', href=re.compile(r'test/\?n=\d+'))
    
    url = link['href']
    query = urlparse(url).query
    print parse_qs(query)['n'][0]  # prints 451484
    

    【讨论】:

      【解决方案2】:

      3 处变化

      • 转义?

      • d+ 括在括号中

      • test\?n=周围放置括号

      示例用法

      >>> import re
      >>> str='<a  href="test/?n=451484"   >'
      >>> re.findall(r'test/\?n=(\d+)', str)
      ['451484']
      

      【讨论】:

      • 谢谢你的回答,但为什么我要把 d+ 放在括号里呢?
      • @david 仅当您使用 findall 时才需要,如我的示例所示。 findall 保存捕获组并打印它们,从而忽略匹配的test.. 并根据需要仅打印数字
      【解决方案3】:

      要搜索文字 ? 字符,您需要使用 \ 对其进行转义。 ? 是正则表达式中的特殊字符,不能(通常)单独使用。

      pattern = r"test/\?n=(\d+)"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-27
        • 2013-08-16
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多