我写了这个正则表达式来匹配所有 href
和 src
HTML 页面中的链接; (我知道我应该使用解析器;这只是试验):
/((href|src)\=\").*?\"/
# Without look-behind
它工作正常,但是当我尝试将表达式的第一部分修改为后视模式时:
/(?<=(href|src)\=\").*?\"/
# With look-behind
它抛出一个错误,指出“无效的后视模式”。有什么想法吗,回溯有什么问题吗?
最佳答案
Lookbehind 有 restrictions :
(?<=subexp) look-behind
(?<!subexp) negative look-behind
Subexp of look-behind must be fixed character length.
But different character length is allowed in top level
alternatives only.
ex. (?<=a|bc) is OK. (?<=aaa(?:b|cd)) is not allowed.
In negative-look-behind, captured group isn't allowed,
but shy group(?:) is allowed.
您不能将备选方案放在(负面)回顾中的非顶层。
将它们放在顶层。您也不需要转义您所做的一些字符。
/(?<=href="|src=").*?"/
关于ruby - 后视正则表达式 (Ruby) 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947331/