关于正则表达式的替换

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

关于正则表达式的替换

Xian Chen
正则表达式中的替换能不能写一个pattern而替换多个不同的字符串,

比如:<table><tr><td>ABCD<br /></td></tr></table>

对于这个字符串来讲,想把其中的<br />替换成'\n',而其它的html标签就替换成空格

要实现这样的功能,正则表达式应该怎么写呢?

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 关于正则表达式的替换

HuaZai.Net
>>> import re
>>> s=r'<table><tr><td>ABCD<br /></td></tr></table>'
>>> p=re.compile(r'<br(?:\s/|)>')
>>> s=p.sub(r'\n',s)
>>> p2=re.compile(r'<[^>]+?>')
>>> p2.sub('',s)
'ABCD\n'
>>>

在08-1-15,Xian Chen <[hidden email]> 写道:
正则表达式中的替换能不能写一个pattern而替换多个不同的字符串,

比如:<table><tr><td>ABCD<br /></td></tr></table>

对于这个字符串来讲,想把其中的<br />替换成'\n',而其它的html标签就替换成空格

要实现这样的功能,正则表达式应该怎么写呢?

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese



--
====================
目前联系方式:
电话:13632845054
QQ:4271520
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 关于正则表达式的替换

@@-2
python好像不能像php那样用表达式数组作为pattern

2008/1/15 欧阳华 <[hidden email]>:
>>> import re
>>> s=r'<table><tr><td>ABCD<br /></td></tr></table>'
>>> p=re.compile(r'<br(?:\s/|)>')
>>> s=p.sub(r'\n',s)
>>> p2=re.compile(r'<[^>]+?>')
>>> p2.sub('',s)
'ABCD\n'
>>>

在08-1-15,Xian Chen <[hidden email]> 写道:
正则表达式中的替换能不能写一个pattern而替换多个不同的字符串,

比如:<table><tr><td>ABCD<br /></td></tr></table>

对于这个字符串来讲,想把其中的<br />替换成'\n',而其它的html标签就替换成空格

要实现这样的功能,正则表达式应该怎么写呢?

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese



--
====================
目前联系方式:
电话:13632845054
QQ:4271520

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 关于正则表达式的替换

大熊
In reply to this post by Xian Chen
sub支持回调函数,可在此根据匹配的字符串进行不同的替换

在08-1-15,Xian Chen <[hidden email]> 写道:
正则表达式中的替换能不能写一个pattern而替换多个不同的字符串,

比如:<table><tr><td>ABCD<br /></td></tr></table>

对于这个字符串来讲,想把其中的<br />替换成'\n',而其它的html标签就替换成空格

要实现这样的功能,正则表达式应该怎么写呢?

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese



--
茫茫人海,你是我的最爱
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese