Now, there is at least one restriction. No lookbehind assertion can be more than 255 characters long. This limit has been around, as nearly as I can tell, ever since lookaround assertions were introduced in 5.005. But it has been lightly documented until now. This restriction means you can not use quantifiers * or +. But bracketed quantifiers are OK, as is ?.
不要点开, 博客网站用的
相关
背景
我的脚本调用了 perl 来做文本处理, 里面有一个正则用到了后置约束:
目的是把不带任何非空前缀的
name
值改为hexh
, 例如:但是却报错了:
方案
根据这篇博客: http://blogs.perl.org/users/tom_wyant/2019/03/native-variable-length-lookbehind.html
那么也就是说对于上述正则:
s/(?<!(.*\\S.*))name:.*/name:\ 'hexh'/gi
的问题就出在了后置约束?<!(.*\\S.*)
中, perl 要求约束中的字符不能用贪婪匹配且少于 255 个字符由此分析, 可以改成类似这样的形式:
?<!({0,127}\\S.{0,127})
, 保证括弧内的字符数量少于等于 255 个即可最终命令如下:
值得一提
值得一提的是, 后置约束对于 perl 来说依然属于实验性功能, 每次用后置断言后它都会有这样的提示: