GNU sed,^和$与|当第一个/最后一个字符匹配时 [英] GNU sed, ^ and $ with | when first/last character matches
问题描述
在REGEXP sed中进行包含类似 ^ |.
之类的替换时,如果第一个字符匹配,则不匹配模式空间开头的空字符串.如果最后一个字符匹配,它也不匹配结尾.为什么会这样?
When doing a substitution that includes something like ^|.
in the REGEXP sed doesn't match the null string at beginning of the pattern space if the first character matches. It also doesn't match the end if the last character matches. Why is that?
以下是使用 123
作为输入(带有 -r
选项)的一些示例:
Here are some examples using 123
as input (with the -r
option):
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
I get the same results when using \`
instead of ^
.
I've tried GNU sed version 4.2.1 and 4.2.2
推荐答案
AFAIK sed将尝试匹配轮流中最长的匹配项.
AFAIK sed will try to match the longest match in an alternation.
因此,如果可以将模式空间开头的空字符串与 1
在同一位置进行匹配.选择了 1
,因为它是最长的匹配项.
So when the null string at the beginning of the pattern space can be matched vs. 1
at the same position. 1
is chosen as it's the longest match.
请考虑以下内容:
$ 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
向下:
The same applies when reaching the end. Lets break sed 's/.\|$/x/g' <<< 123
down:
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.
这篇关于GNU sed,^和$与|当第一个/最后一个字符匹配时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!