gpt4 book ai didi

regex - 在 sed 中替换/删除匹配字符串中的特殊字符

转载 作者:行者123 更新时间:2023-12-04 18:09:51 25 4
gpt4 key购买 nike

我有一个包含类似行的文件

I want a lot <*tag 1> more <*tag 2>*cheese *cakes.

我正在尝试删除 <> 中的 *但不是在外面。标签可能比上面的更复杂。例如, <*better *tag 1> .

我试过 /\bregex\b/s/\*//g ,它适用于标签 1 但不适用于标签 2。那么我怎样才能让它也适用于标签 2?

非常感谢。

最佳答案

如果标签中只有一个星号,那么简单的解决方案

sed 's/<\([^>]*\)\*\([^>]*\)>/<\1\2>/g'

如果你可以有更多,你可以使用 sed goto 标签系统
sed ':doagain s/<\([^>]*\)\*\([^>]*\)>/<\1\2>/g; t doagain'

其中 doagain 是循环标签,t doagain 是条件跳转到标签 doagain。引用sed手册:
t label

Branch to label only if there has been a successful substitution since the last
input line was read or conditional branch was taken. The label may be omitted, in
which case the next cycle is started.

关于regex - 在 sed 中替换/删除匹配字符串中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16841943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com