当进行包含类似^|.REGEXP 的替换时,如果第一个字符匹配,则 sed 与模式空间开头的空字符串不匹配。如果最后一个字符匹配,它也不匹配结尾。这是为什么?
以下是一些123用作输入的示例(带有-r选项):
substitution expected output actual output comments
s/^/x/g x123 x123 works as expected
s/$/x/g 123x 123x works as expected
s/^|$/x/g x123x x123x works as expected
s/^|./x/g xxxx xxx didn't match the very begining
s/.|$/x/g xxxx xxx didn't match the very end
s/^|1/x/g xx23 x23 didn't match the very begining
s/^|2/x/g x1x3 x1x3 this time it did match the begining
使用\`而不是^.
我试过 GNU sed 版本 4.2.1 和 4.2.2