html6="""
<p<ins style="background:#e6ffe6;">re><code</ins>>
int aint bint c<ins style="background:#e6ffe6;"></code></ins></p<ins style="background:#e6ffe6;">re</ins>><p>int d</p>
"""
Html6和Html7是一样的,只是Html7有"\n"
html7="""
<p<ins style="background:#e6ffe6;">re><code</ins>>int a
int b
int c<ins style="background:#e6ffe6;">
</code></ins></p<ins style="background:#e6ffe6;">re</ins>>
<p>int d</p>
"""
p_to_pre_code_pattern = re.compile(
"""<p
<(?P<action_tag>(del|ins)) (?P<action_attr>.*)>re><code</(?P=action_tag)>
>
(?P<text>.*?)
<(?P=action_tag) (?P=action_attr)>
</code></(?P=action_tag)>
</p
<(?P=action_tag) (?P=action_attr)>re</(?P=action_tag)>
>""",re.VERBOSE)
print re.match(p_to_pre_code_pattern,html6)
print re.match(p_to_pre_code_pattern,html7)
html6 和 html7 都不匹配? , 但如果我将 "\n"替换为 "",两者都会有很多。
print re.match(p_to_pre_code_pattern,html6.replace("\n",""))
print re.match(p_to_pre_code_pattern,html7.replace("\n",""))
我想知道我应该如何更改 p_to_pre_code_pattern
以便在不调用 replace("\n",""))
的情况下同时匹配 html6 和 html7?
最佳答案
也许你在调用 re.compile(..., re.VERBOSE|re.DOTALL)< 时错过了
re.DOTALL
标志
re.S
re.DOTALL
Make the '.' special character match any character at all, including a newline;
without this flag, '.' will match anything except a newline.
关于Python重新匹配空格和新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9536313/