【问题标题】:Python regex look-behind requires fixed-width patternPython regex look-behind 需要固定宽度的模式
【发布时间】:2011-02-06 11:31:57
【问题描述】:

在尝试提取 html 页面的标题时,我总是使用以下正则表达式:

(?<=<title.*>)([\s\S]*)(?=</title>)

这将提取文档中标签之间的所有内容并忽略标签本身。但是,当尝试在 Python 中使用此正则表达式时,会引发以下异常:

Traceback (most recent call last):  
File "test.py", line 21, in <module>
    pattern = re.compile('(?<=<title.*>)([\s\S]*)(?=</title>)')
File "C:\Python31\lib\re.py", line 205, in compile
    return _compile(pattern, flags)   
File "C:\Python31\lib\re.py", line 273, in _compile
    p = sre_compile.compile(pattern, flags)   File
"C:\Python31\lib\sre_compile.py", line 495, in compile
    code = _code(p, flags)   File "C:\Python31\lib\sre_compile.py", line 480, in _code
_compile(code, p.data, flags)   File "C:\Python31\lib\sre_compile.py", line 115, in _compile
    raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern

我使用的代码是:

pattern = re.compile('(?<=<title.*>)([\s\S]*)(?=</title>)')
m = pattern.search(f)

如果我做一些最小的调整,它会起作用:

pattern = re.compile('(?<=<title>)([\s\S]*)(?=</title>)')
m = pattern.search(f)

但是,这不会考虑由于某种原因具有属性或类似属性的潜在 html 标题。

有人知道解决此问题的好方法吗?任何提示都表示赞赏。

【问题讨论】:

  • 是否有某些原因必须是零宽度断言?你可以只使用一个非捕获组吗?
  • 虽然你不应该使用正则表达式来处理 HTML。为什么你完全使用环视而不是像&lt;title.*&gt;([\s\S]*)&lt;/title&gt;这样的东西来参加第一组的比赛?

标签: python html regex


【解决方案1】:

放弃使用正则表达式解析 HTML 的想法,改用实际的 HTML 解析库。经过快速搜索,我找到了this one。这是从 HTML 文件中提取信息的一种更安全的方法。

请记住,HTML 不是正则语言,因此正则表达式从根本上来说是错误的从中提取信息的工具。

【讨论】:

【解决方案2】:

Here's a famous answer 使用正则表达式解析 html,这句话很好地说明了“不要使用正则表达式来解析 html”。

【讨论】:

  • 是和不是。您不应该使用正则表达式来解析整个 DOM,或者复杂的标签嵌套。但是,正如 OP 试图做的那样,解析单个非嵌套标签是对正则表达式的完全合法使用。
【解决方案3】:

如果只想获取title标签,

html=urllib2.urlopen("http://somewhere").read()
for item in html.split("</title>"):
    if "<title>" in item:
        print item[ item.find("<title>")+7: ]

【讨论】:

    【解决方案4】:

    类似的东西呢:

     r = re.compile("(<title.*>)([\s\S]*)(</title>)")
     title = r.search(page).group(2)
    

    【讨论】:

      【解决方案5】:

      提取非嵌套HTML/XML标签内容的正则表达式其实很简单:

      r = re.compile('<title[^>]*>(.*?)</title>')
      

      但是,对于更复杂的事情,您应该使用适当的 DOM 解析器,例如 urllib 或 BeautifulSoup。

      【讨论】:

        猜你喜欢
        • 2020-02-07
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 2011-09-01
        • 1970-01-01
        • 2014-09-12
        • 2021-02-21
        相关资源
        最近更新 更多