[论文解读] Derivative Based Extended Regular Expression Matching Supporting Intersection, Complement and Lookarounds
本文提出了一种基于符号导数的正则表达式引擎,通过扩展标准正则表达式以支持交集、补集以及正向和负向前瞻与后瞻(lookaheads 和 lookbacks),实现了精确、高效且语义一致的匹配。该方法利用匹配集上的有效布尔代数,通过布尔重写规则实现优化,证明了在传统回溯方法因阶乘级膨胀而无法解决的问题上具有可行性。
Regular expressions are widely used in software. Various regular expression engines support different combinations of extensions to classical regular constructs such as Kleene star, concatenation, nondeterministic choice (union in terms of match semantics). The extensions include e.g. anchors, lookarounds, counters, backreferences. The properties of combinations of such extensions have been subject of active recent research. In the current paper we present a symbolic derivatives based approach to finding matches to regular expressions that, in addition to the classical regular constructs, also support complement, intersection and lookarounds (both negative and positive lookaheads and lookbacks). The theory of computing symbolic derivatives and determining nullability given an input string is presented that shows that such a combination of extensions yields a match semantics that corresponds to an effective Boolean algebra, which in turn opens up possibilities of applying various Boolean logic rewrite rules to optimize the search for matches. In addition to the theoretical framework we present an implementation of the combination of extensions to demonstrate the efficacy of the approach accompanied with practical examples.
研究动机与目标
- 开发一种在符号导数框架下支持交集、补集和前瞻后瞻的正则表达式引擎。
- 证明这些扩展的组合能够形成在布尔运算下封闭的匹配语义,构成有效的布尔代数。
- 为此前因回溯或标准非回溯引擎无法处理的问题提供实用且高性能的正则表达式匹配。
- 证明并实现一个猜想:所有上下文敏感的锚点(例如单词边界)均可通过前瞻后瞻表达。
- 为通过布尔重写规则和高效缓存技术实现正则表达式匹配优化提供基础。
提出的方法
- 本文将 .NET 7 的非回溯正则表达式引擎扩展为支持符号导数,以实现对交集(&)、补集(~)和前瞻后瞻(?=, ?!, ?<=, ?<!)的支持。
- 提出了基于符号导数的理论框架,确保匹配语义的正确性和确定性。
- 证明了匹配集语义构成有效的布尔代数,从而可系统性地应用布尔重写规则进行优化。
- 实现采用基于导数的求值引擎,通过计算正则表达式相对于输入字符的导数,追踪匹配位置和谓词。
- 前瞻后瞻通过在每个位置评估前瞻/后瞻条件实现,不消耗输入字符,使用导出的谓词判断匹配有效性。
- 提出通过缓存技术优化前瞻后瞻,当遇到如换行符等使条件失效的字符时,存储并使谓词失效(例如 ψ 表示字符类)。

实验结果
研究问题
- RQ1在不牺牲确定性或性能的前提下,能否在基于符号导数的正则表达式引擎中合理组合交集、补集和前瞻后瞻?
- RQ2这些扩展的组合是否能形成在布尔运算下封闭的匹配语义,构成有效的布尔代数?
- RQ3该扩展引擎能否解决导致传统回溯引擎出现阶乘级膨胀的问题,例如定位任意顺序匹配的子串?
- RQ4是否如先前工作所猜想的那样,所有上下文敏感的锚点(如单词边界)均可仅通过前瞻后瞻表达?
- RQ5布尔重写规则和缓存技术在复杂扩展的正则表达式匹配中,能在多大程度上提升性能?
主要发现
- 扩展后的正则表达式引擎成功处理了导致 .NET 7 回溯和非回溯引擎出现阶乘级膨胀的任意顺序匹配定位问题。
- 扩展正则表达式系统的匹配语义构成有效的布尔代数,支持通过布尔重写规则进行系统性优化。
- 所有上下文敏感锚点,包括单词边界和行锚点,均可仅通过前瞻后瞻表达,且该结论已正式证明并实现。
- 该引擎在短上下文前瞻后瞻(如 ^, $, \b)情况下表现出线性时间复杂度,而较长的前瞻后瞻(如 (?<=a.*)b)可通过谓词缓存优化,避免二次方行为。
- 通过补集和交集可模拟正则表达式的惰性匹配,例如 a[^b\n]*b 或 (a.*)&(~(.*b.*)b),在不使用惰性量词的情况下提供等价语义。
- 实现重用了 .NET 7 非回溯引擎的后端,并通过新增操作符扩展其功能,在保持性能和正确性的同时支持了新的使用场景。

更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。