2012-03-02 10 views
2
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" はPythonの再試合

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","")) 

私はreplace("\n",""))を呼び出さずにhtml6とHTML7の両方にマッチしますp_to_pre_code_patternを変更する方法を知りたいですか?

+0

は、私があまりにも最新のウェブのものではないんだけど、 '美しいsoup'は、このためのツールではないだろうというとき?たぶん、あなたはre.DOTALLフラグを欠場 – Jeff

+0

パターンに空白を追加する必要があります。[この回答](http://stackoverflow.com/questions/4590298/how-to-ignore-whitespace-in-a-regular-expression-subject-string)が適切と思われます。 – ChrisP

答えて

1

コールre.compile(..., re.VERBOSE|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. 
+0

はい、私はそれを見つけました。これに加えて、 '.'はデフォルトで改行にマッチしません。 – jianjun