2

当进行包含类似^|.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

在线尝试!

4

1 回答 1

4

AFAIK sed 将尝试交替匹配最长的匹配项。

因此,当模式空间开头的空字符串可以与1相同位置匹配时。1被选择,因为它是最长的匹配。

考虑以下:

$ sed 's/12\|123/x/g' <<< 123
x
$ sed 's/123\|12/x/g' <<< 123
x
$ sed 's/^1\|12/x/g' <<< 123
x3

到达终点时也是如此。让我们分解sed 's/.\|$/x/g' <<< 123

123
^
. matches and replace with x
x23
 ^
 . matches and replace with x
xx3
  ^
  . matches and replace with x
xxx
   ^
   Out of pattern space $ will not match.
于 2016-10-01T16:46:25.460 回答