使用sed和正则表达式替换HTML内容。

3

我正在尝试在bash脚本中使用sed替换一些HTML内容。但由于某种原因,我没有得到正确的结果, regex部分没有起到替换的作用。

需要替换的HTML:

<h3 class="indicate-hover css-5fzt5q">For the Most Complex Heroines Animation
<h3 class="indicate-hover css-1pvrrwb">The Psychology Behind Sibling

to

 head For the Most Complex Heroines Animation
 head The Psychology Behind Sibling

我使用了

  sed -e 's/<h3 class="indicate-hover css-([a-b0-9]+)">/head/g'

主要是 ([a-b0-9]) 这部分执行失败了,我可能漏掉了些东西,我想让它更具体,我有 "<p class="summary-class css-1azn4ub">How many words can" ,我想将其替换为“tail”并使用许多其他标记。正则表达式部分让我很痛苦。


1
我尝试了:sed -E 's/<h3 class="indicate-hover css-([a-b0-9])">/head/g' 和 sed -e 's/<h3 class="indicate-hover css-([a-b0-9]\+)">/head/g',但仍然没有令人满意的结果。 - Debankan
2个回答

1
使用 sed 命令
$ sed 's/.*-[[:alnum:]]\+">/head /' input_file

输出

head For the Most Complex Heroines Animation
head The Psychology Behind Sibling

实际上我想让它更具体,我有一个"<p class="summary-class css-1azn4ub">How many words can",我想将其替换为“tail”,还有很多其他标签。正则表达式部分让我感到痛苦。 - Debankan
1
@Debankan 请检查编辑 - HatLess

0

在编程中,除非你使用sed -E,否则你需要使用\+

  • \+是默认的基本正则表达式中的有效量词。
  • +是扩展正则表达式中的有效量词。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接