繁体   English   中英

Python和re.compile返回不一致的结果

[英]Python and re.compile return inconsistent results

我正在尝试将href="../directory"所有实例替换为href="../directory/index.html"

在Python中,

reg = re.compile(r'<a href="../(.*?)">')
for match in re.findall(reg, input_html):
    output_html = input_html.replace(match, match+'index.html')

产生以下输出:

href="../personal-autonomy/index.htmlindex.htmlindex.htmlindex.html"  
href="../paternalism/index.html"  
href="../principle-beneficence/index.htmlindex.htmlindex.html"  
href="../decision-capacity/index.htmlindex.htmlindex.html" 

知道为什么它可以与第二个链接一起使用,但其他链接却不起作用吗?

来源的相关部分:

<p> 

 <a href="../personal-autonomy/">autonomy: personal</a> |
 <a href="../principle-beneficence/">beneficence, principle of</a> |
 <a href="../decision-capacity/">decision-making capacity</a> |
 <a href="../legal-obligation/">legal obligation and authority</a> |
 <a href="../paternalism/">paternalism</a> |
 <a href="../identity-personal/">personal identity</a> |
 <a href="../identity-ethics/">personal identity: and ethics</a> |
 <a href="../respect/">respect</a> |
 <a href="../well-being/">well-being</a> 

</p> 

编辑 :重复的'index.html'实际上是多个匹配项的结果。 (例如href =“ ../ personal-autonomy / index.htmlindex.htmlindex.htmlindex.html”是因为在原始源中四次发现../personal-autonomy)。

作为一般的正则表达式问题,如何在不向所有匹配项添加额外的“ index.html”的情况下替换所有实例?

不要用正则表达式解析html:

import re    
from lxml import html

def replace_link(link):
    if re.match(r"\.\./[^/]+/$", link):
        link += "index.html"
    return link

print html.rewrite_links(your_html_text, replace_link)

输出量

<p> 

 <a href="../personal-autonomy/index.html">autonomy: personal</a> |
 <a href="../principle-beneficence/index.html">beneficence, principle of</a> |
 <a href="../decision-capacity/index.html">decision-making capacity</a> |
 <a href="../legal-obligation/index.html">legal obligation and authority</a> |
 <a href="../paternalism/index.html">paternalism</a> |
 <a href="../identity-personal/index.html">personal identity</a> |
 <a href="../identity-ethics/index.html">personal identity: and ethics</a> |
 <a href="../respect/index.html">respect</a> |
 <a href="../well-being/index.html">well-being</a> 

</p>

我想我发现了问题

reg = re.compile(r'<a href="../(.*?)">')

for match in re.findall(reg, input_html):

output_html = input_html.replace(match, match+'index.html')

在这里,'input_html'在for循环中被修改,然后再次在同一个'input_html'中搜索正则表达式,这是一个bug :)

让你的前两个逃脱.

reg = re.compile(r'<a[ ]href="[.][.]/(.*?)">')

但是我会尝试使用lxml代替。

问题是,a标签的内容也与您尝试替换的内容匹配。

这绝不是理想的方法,但是我认为,如果将regex替换为:

reg = re.compile(r'<a href="(\.\./.*?)">')

正则表达式中有一个错误,因为..不匹配两个点。 相反,它是. 元字符 要表示一个点,您需要将其转义。

您的正则表达式应为: <a href="\\.\\./(.*?)"

此外,假设所有您的href的形式为../somedirectory/你可以逃脱一个简单的正则表达式:

for match in re.compile(r'<a href="(.*?)"').findall(html):
    html = html.replace(match, match + "index.html")

在这里,正则表达式匹配

<a href="    # start of the taf and attribute
(            # start of a group
 .*          # any character, any number of times
)            # end of group
"            # end of the attribute

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM