html - 正则表达式问题匹配 HTML 标记

标签 html regex linux bash sed

所以我正在尝试使用 sed(它必须在这些系统上使用 sed,所以请不要只推荐使用 Perl)来匹配 HTML 标记并从中获取内容。 HTML 标签看起来像这样:

<div class="SectionText"> Received poor service or think your current mechanic is ripping you off? Get some help from <a href="http://www.union.umd.edu/gradlegalaid/index.htm" target="_blank">Graduate Legal Aid</a> or consult the <a href="http://www.oag.state.md.us/Consumer/index.htm" target="_blank">Maryland Attorney General Office of Consumer Protection</a> at <a href="mailto:consumer@oag.state.md.us">consumer@oag.state.md.us</a> or through their hotline at 410-528-8662 or 888-743-0023.<br /></div>

全部在一条线上。所以,我写了这个...但是它不起作用。

sed 's/<div class=\"SectionText\">\([^<\/div>]*\)<\/div>/\1/g'

这不会改变任何文本。

我尝试使用此网站作为指南 - http://www.ibm.com/developerworks/linux/library/l-sed2/index.html (在 RegExp Snafus 下)\

最重要的是这一行脚本不要贪心,直到最后才匹配

最佳答案

除了尝试在 html 上使用正则表达式(参见 RegEx match open tags except XHTML self-contained tags),我看到的第一个问题是:

[^<\/div>]*

这是说匹配任何不是 <字符 , / , d , i , v , 或 > .很明显,你有一个 d和一个 i在那里。 (“收到 d 糟糕的服务......”)

如果您准备为此使用正则表达式,并且您有一个非常 受控/可预测的输入,您可以简单地执行 [^<>] ,假设您的文本不会包含这些字符。但是,我看到你这样做了,因为你的 div 里面有标签...

但是,如果你这样做:

sed 's/<div.class="SectionText">\(.*\)<\/div>/\1/g'

只要您没有多个 </div> 它就应该可以工作秒。 .*只会匹配直到找到 <\/div> .

关于html - 正则表达式问题匹配 HTML 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9744246/

相关文章:

javascript - 如何获取特殊字符后的最后一个单词?

php - 不保存到json文件(php+redis)?

c++ - 基于共享内存的聊天应用程序的问题

c# - HTML 未显示在 outlook 邮件中

html - 使用 Bootstrap - 如何制作 2 行,每行有两个水平列,在保持响应的同时具有相同的高度

javascript 正则表达式 切换菜单

python - 如何从网址中提取标题?

c++ - 无法理解核心文件分析的 GDB x 命令输出

c# - 如何在 C# 中使用 foreach inside string?

html - 如何移动特定元素?