Perl正则表达式量化错误预先确定

Mat*_*ijo 3 regex perl grep

当我在precced中使用量化时,返回此错误,请参阅多个示例:

printf 'Joe Satriani\nWhitney Houston\n' | grep -Poi '(?<=J\w)[\w\s]+'
e Satriani

printf 'Joe Satriani\nWhitney Houston\n' | grep -Poi '(?<=J\w+)[\w\s]+' 
grep: lookbehind assertion is not fixed length

printf 'Joe Satriani\nWhitney Houston\n' | grep -Poi '(?<=J\w{2})[\w\s]+' 
Satriani
Run Code Online (Sandbox Code Playgroud)

我不能在预先使用量化?

ike*_*ami 5

在Perl中,lookbehind可以匹配的所有字符串必须具有相同的长度.显然,你的grep工具也一样.

在Perl中,您的问题将通过捕获来解决.

say $1 if /J\w+\s+(\w[\w\s]*)/;
Run Code Online (Sandbox Code Playgroud)

在Perl中,\K在进行替换时经常有用来解决类似的问题,并且看起来它也受到支持grep!

$ printf 'Joe Satriani\nWhitney Houston\n' | grep -Poi 'J\w+\s+\K\w[\w\s]*'
Satriani
Run Code Online (Sandbox Code Playgroud)

我印象深刻!