如何用正则表达式替换智能标签



我想用SED替换智能标签,但是我无法克服这个问题

这是我目前得到的:

echo "This {$is} a test to replace a tag" | sed -e 's/\{$is\}/was/g'

这是前一个命令的结果:

This {} a test to replace a tag

这是我实际想要归档的一个简单的替换{$tags}

This was a test to replace a tag

可能您正在使用bash,它看到$is并试图将其替换为变量。如果您将字符串括在单引号中,它将被视为文字。此外,您还向regex表达式添加了一大堆额外的转义。echo 'This {$is} a test to replace a tag' | sed -e 's/{$is}/was/'将返回您期望的内容。

使用简单引号(用双引号$is代替bash变量$is的内容,即"):

echo 'This {$is} a test to replace a tag' | sed 's/{$is}/was/g'

最新更新