如何使用grep匹配具有属性的HTML特定标签之间的内容? [英] How to match content between HTML specific tags with attribute using grep?

查看:49
本文介绍了如何使用grep匹配具有属性的HTML特定标签之间的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想匹配标签 <div class="Message"> 中包含的文本,我应该将哪个正则表达式用于命令 grepHTML 文件中的结束标记 </div>?

解决方案

这是使用 GNU grep 的一种方法:

grep -oP '(?<=

).*?(?= </div>)' 文件

如果您的标签跨越多行,请尝试:

<代码><文件 tr -d '
' |grep -oP '(?<=<div class="Message">).*?(?= </div>)'

Which regular expression should I use with the command grep if I wanted to match the text contained within the tag <div class="Message"> and its closing tag </div> in an HTML file?

解决方案

Here's one way using GNU grep:

grep -oP '(?<=<div class="Message"> ).*?(?= </div>)' file

If your tags span multiple lines, try:

< file tr -d '
' | grep -oP '(?<=<div class="Message"> ).*?(?= </div>)'

这篇关于如何使用grep匹配具有属性的HTML特定标签之间的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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