清除xml中的CData标签 正则

ibkon_hui 2010-08-28 05:11:26
求 清除xml中的CData标签的正则

<content height="18" width="1202" text-color="" link-color="" vlink-color=""><![CDATA[<div id="AutoGeneratedID-1">sdfsdfsdf</div>]]></content>
...全文
371 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
notax 2010-08-29
  • 打赏
  • 举报
回复
same same

>>> import re
>>> st = """<content height="18" width="1202" text-color="" link-color="" vlink-color=""><![CDATA[<div id="AutoGeneratedID-1">sdfsdfsdf</div>]]></content>"""
>>> regexp = re.compile("<!\[CDATA\[(?P<text>[^\]]*)\]\]>")
>>> print regexp.sub('\g<text>', st)
<content height="18" width="1202" text-color="" link-color="" vlink-color=""><div id="AutoGeneratedID-1">sdfsdfsdf</div></content>
lokii 2010-08-28
  • 打赏
  • 举报
回复
import re

st = """<content height="18" width="1202" text-color="" link-color="" vlink-color=""><![CDATA[<div id="AutoGeneratedID-1">sdfsdfsdf</div>]]></content>"""
pat1 = re.compile("<!\[CDATA\[([^\]]+)\]\]>")
m = pat1.search(st)
if m:
st = st.replace(m.group(0), m.group(1))

print st

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧