Jik*_*ose 10 ruby regex lookbehind
我写了这个正则表达式来匹配HTML页面中的所有href和src链接; (我知道我应该使用解析器;这只是试验):
/((href|src)\=\").*?\"/#没有后视
它工作正常,但当我尝试将表达式的第一部分修改为后视模式时:
/(?<=(href|src)\=\").*?\"/#带有后视
它会抛出一个错误,指出"无效的后视模式".任何想法,看起来有什么问题?
saw*_*awa 17
Lookbehind有限制:
(?<=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.
Run Code Online (Sandbox Code Playgroud)
你不能在(负面)外观中将替代品放在非顶级.
把它们放在最顶层.你也不需要逃避你所做的一些角色.
/(?<=href="|src=").*?"/
Run Code Online (Sandbox Code Playgroud)