在 Go 中匹配 html 标签之外的 html 文本的最佳方法是什么?
时间:2024-04-07
浏览:157
作者:佚名
今天golang学习网给大家带来了《在 Go 中匹配 html 标签之外的 html 文本的最佳方法是什么?》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~
问题内容
我正在解析一堆 html,如果某些 <a> 标签包含某些文本,我需要删除它们。通常,我会使用 goquery,但我正在搜索的文本通常位于 html 标签本身之外。例如,这个 html:
<html><body> this is the start. <a href="http://example.com/path">we don't want to match this text.</a> <a href="http://www.example.com/another/path" style="font-family:arial, helvetica, 'sans-serif'; color:#838383;font-size:12px; line-height:14px"></a> match this text.<a href="blah">we also don't want to match this text</a> </body></html>
我正在使用这个正则表达式,但它失败并匹配我不想匹配的文本:
(?is)<a[^>]+href=["'](?P<link>.*?)["']*.?> match this text\.
https://regex101.com/r/iexpqc/1
解决方案
xmlstarlet ed -d '//a[contains(text(), "want to match")]' file.html
输出
<?xml version="1.0"?> <html> <body> This is the start. <a href="http://www.example.com/another/path" style="font-family:Arial, Helvetica, 'sans-serif'; color:#838383;font-size:12px; line-height:14px"/> match this text. </body> </html>
注意
- 如果您想即时替换,请添加
-l开关
今天关于《在 Go 中匹配 html 标签之外的 html 文本的最佳方法是什么?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
