【发布时间】:2011-07-13 05:03:01
【问题描述】:
我正在使用 Beautiful Soup 将出现的模式替换为 HTML 文件中的 href 链接
我遇到了如下所述的问题
modified_contents = re.sub("([^http://*/s]APP[a-z]{2}[0-9]{2})", "<a href=\"http://stack.com=\\1\">\\1</a>", str(soup))
示例输入 1:
Input File contains APPdd34
Output File contains <a href="http://stack.com=APPdd34"> APPdd34</a>
示例输入 2:
Input File contains <a href="http://stack.com=APPdd34"> APPdd34</a>
Output File contains <a href="http://stack.com=<a href="http://stack.com=APPdd34"> APPdd34</a>"> <a href="http://stack.com=APPdd34"> APPdd34</a></a>
所需的输出文件 2 与示例输入文件 2 相同。
我该如何解决这个问题?
【问题讨论】:
-
你需要的不是
[^...],而是一个negative lookbehind assertion(最后是一个negative lookahead assertion)。在 Python 手册中阅读它。 -
您可能会发现pythex 很有用,它可以让您实时测试您的python 正则表达式。
标签: python html find beautifulsoup