Java Regex或XML解析器? [英] Java Regex or XML parser?

查看:43
本文介绍了Java Regex或XML解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除所有标签,例如

I want to remove any tags such as

<p>hello <namespace:tag : a>hello</namespace:tag></p>

成为

 <p> hello hello </p>

如果正则表达式是正则表达式,那么最好的方法是什么呢?有人可以帮忙吗?

What is the best way to do this if it is regex for some reason this is now working can anyone help?

(<|</)[:]{1,2}[^</>]>

添加了

推荐答案

出于这些目的,您不应使用正则表达式,而应使用 lxml BeautifulSoup

You should not use regex for these purposes use a parser like lxml or BeautifulSoup

>>> import lxml.html as lxht
>>> myString = '<p>hello <namespace:tag : a>hello</namespace:tag></p>'
>>> lxht.fromstring(myString).text_content()
'hello hello'

这是原因的原因不能使用正则表达式解析html/xml.

Here is a reason why you should not parse html/xml with regex.

这篇关于Java Regex或XML解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆