首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python正则怎么去除字符串

2012-03-09 
python正则如何去除字符串比方说我有一段文本,sdivtest/divimg srcaaa.jpga href#test/a

python正则如何去除字符串
比方说我有一段文本,s="<div>test</div><img src='aaa.jpg'><a href=#>test</a><span>www.csdn.net</span><table>someother</table>",我想去除除<img>标签和<a>标签以外的所有标签,即经过处理后得到的字符串应该是"test<img src='aaa.jpg'><a href=#>test</a>www.csdn.netsomeother",python的正则感觉好别扭,求大神指教

[解决办法]

探讨

就是匹配所有不是某些特定字符串的字符串,再比方说str="this <cat>is<dog> a test",我想要匹配除"<cat>"和"<dog>"以外的所有字符,结果应该为"this is a test"

[解决办法]
二楼说的方式应该可以了...
Python code
import redef repl(m):    if m.group(1).strip() in ('cat', 'dog'):        return ''    return m.group(0)    s = 'this <cat>is<dog> a test<pig>'print re.sub(r'<(.*?)>', repl, s) 

热点排行