“lookbehind assertion MUST be fixed length"的技术原因是什么?在正则表达式中? [英] What's the technical reason for "lookbehind assertion MUST be fixed length" in regex?

查看:201
本文介绍了“lookbehind assertion MUST be fixed length"的技术原因是什么?在正则表达式中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,下面的正则表达式会导致失败报告lookbehind assertion is not fixed length:

For example,the regex below will cause failure reporting lookbehind assertion is not fixed length:

#(?<!(?:(?:src)|(?:href))=["\']?)((?:https?|ftp)://[^\s\'"<>()]+)#S

前瞻不存在这种限制.

推荐答案

Lookahead 和 Lookbehind 并不像它们的名字所暗示的那样相似.前瞻表达式的工作方式与独立正则表达式完全相同,只是它锚定在当前匹配位置并且不消耗匹配的内容.

Lookahead and lookbehind aren't nearly as similar as their names imply. The lookahead expression works exactly the same as it would if it were a standalone regex, except it's anchored at the current match position and it doesn't consume what it matches.

回顾是一个完全不同的故事.从当前匹配位置开始,它一次一个字符地向后遍历文本,尝试在每个位置匹配其表达式.在不可能匹配的情况下,lookbehind 必须在它放弃之前一直走到文本的开头(一次一个字符,记住).将其与仅应用一次的前瞻表达式进行比较.

Lookbehind is a whole different story. Starting at the current match position, it steps backward through the text one character at a time, attempting to match its expression at each position. In cases where no match is possible, the lookbehind has to go all the way to the beginning of the text (one character at a time, remember) before it gives up. Compare that to the lookahead expression, which gets applied exactly once.

当然,这是一种粗略的过度简化,并非所有口味都这样,但您明白了.后视的应用方式与前瞻的应用方式有着根本的不同(而且效率远低于).只有限制后视必须向后看多远才有意义.

This is a gross oversimplification, of course, and not all flavors work that way, but you get the idea. The way lookbehinds are applied is fundamentally different from (and much, much less efficient than) the way lookaheads are applied. It only makes sense to put a limit on how far back the lookbehind has to look.

这篇关于“lookbehind assertion MUST be fixed length"的技术原因是什么?在正则表达式中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆