GNU sed,^和$ with |当第一个/最后一个字符匹配时

时间:2016-10-01 15:27:29

标签: bash sed gnu-sed

当在第一个字符匹配时,在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

Try it online!

1 个答案:

答案 0 :(得分: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.