【发布时间】:2017-02-10 00:35:01
【问题描述】:
当在 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
【问题讨论】: