小编Ste*_*e H的帖子

使用 sed 替换为第一个匹配字符

试图更新一些乳胶代码。四行:

something $^\text{TT}$ and more stuff
something $^\text{T}$ and more stuff
something $^\text{M\times N}$ and more stuff
something $^\text{T} {otherstuff}$
Run Code Online (Sandbox Code Playgroud)

应该变成

something $^{\text{TT}}$ and more stuff
something $^{\text{T}}$ and more stuff
something $^{\text{M\times N}}$ and more stuff
something $^{\text{T}} {otherstuff}$
Run Code Online (Sandbox Code Playgroud)

换句话说,用额外的{...}.

我使用的尝试sed如下

sed 's/\^\\text{\(.*\)}/\^{\\{text{\1}}/' testregex
Run Code Online (Sandbox Code Playgroud)

这适用于前三行,但最后一行不起作用并产生

something $^{\text{T} {otherstuff}}$
Run Code Online (Sandbox Code Playgroud)

反而。问题是,sed是最后一个匹配}的每一行,但我需要它匹配第一个}\text{

此外,如果这可以在同一条线上多次工作,那就太好了,例如,

^\text{1} la la la ^\text{2}
Run Code Online (Sandbox Code Playgroud)

应该变成

^{\text{1}} la la la ^{\text{2}}
Run Code Online (Sandbox Code Playgroud)

我敢肯定只有一个小小的修改就可以使它工作,但我无法弄清楚,这让我发疯。提前致谢!

sed text-processing regular-expression

6
推荐指数
2
解决办法
209
查看次数

标签 统计

regular-expression ×1

sed ×1

text-processing ×1