Perl正则表达式启动线锚失败

geo*_*ory 0 regex perl

我想在示例的第二行更改"this",但它没有发生.非常感谢我出错的想法.

echo "not this but
this one" > test.txt
perl -0777 -i -pe 's/^this/rhinoceros/igs' test.txt
cat test.txt
not this but
this one
Run Code Online (Sandbox Code Playgroud)

Bor*_*din 5

替换时你有所有错误的修饰符.你可能只想做一个改变,所以这/g是不必要的; 要匹配的文本是完全匹配的this,所以这/i是不必要的,并且您.的模式中没有点字符,因此/s不执行任何操作

你是什么需要的是/m(多线)修改,以使^比赛在字符串的中间线的开始,以及刚刚在字符串的开始

这应该适合你

perl -0777 -i -pe 's/^this/rhinoceros/m' test.txt
Run Code Online (Sandbox Code Playgroud)