paperbot · PL 论文追踪

全部论文 待读 收藏

P2 Efficient Matching of Regular Expressions with Lookaround Assertions

带环视断言的正则表达式的高效匹配
POPL 8(POPL)2024匹配度 2/10 · 论文研究带环视断言正则表达式的形式语义与高效匹配算法,属于形式语言/自动机方向,虽然具有形式化和工程实现,但与用户关注的 PL↔AI、静态分析、浮点程序分析、贝叶斯程序分析及 LLM/Agent 交叉方向没有直接交集。引用 18 inbox
Konstantinos Mamouras, Agnishom Chattopadhyay
crossref: 2026-09-10

AI 速览

一句话:提出带环视断言正则表达式的形式语义和 O(m·n) 匹配算法,并用 oracle-NFA 实现,避免回溯导致的超线性时间。

正则表达式常扩展 lookaround(先行/后顾)断言,用于根据上下文限定匹配。现有实现多依赖回溯,运行时间可能随输入长度超线性增长。本文首先给出 lookaround 的形式数学语义,并据此建立若干等式化简性质;随后提出自底向上求值 lookaround 的匹配算法,时间复杂度为 O(m·n),其中 m 为正则表达式大小、n 为输入文本长度。算法基于新定义 oracle-NFA,其 ε-转移由提供各位置 lookaround 真值的 oracle 查询保护。作者实现该算法并加入三项性能优化,与 PCRE 和 Java 正则库对比,结果表明其不会出现超线性运行时间且快数倍。

问题
带 lookaround 断言的正则表达式匹配在现有回溯实现下可能产生超线性运行时间,缺乏统一的形式语义与高效算法。
方法
论文给出 lookaround 的形式语义与等式性质,定义由 oracle 查询保护的 oracle-NFA,并自底向上求值断言,实现 O(m·n) 匹配算法及优化实现。
评估
在 PCRE 和 Java regex 库上进行实验比较,验证实现避免超线性时间且快数倍。
原文摘要(Abstract)

Regular expressions have been extended with lookaround assertions, which are subdivided into lookahead and lookbehind assertions. These constructs are used to refine when a match for a pattern occurs in the input text based on the surrounding context. Current implementation techniques for lookaround involve backtracking search, which can give rise to running time that is super-linear in the length of input text. In this paper, we first consider a formal mathematical semantics for lookaround, which complements the commonly used operational understanding of lookaround in terms of a backtracking implementation. Our formal semantics allows us to establish several equational properties for simplifying lookaround assertions. Additionally, we propose a new algorithm for matching regular expressions with lookaround that has time complexity O m ⋅ n , where m is the size of the regular expression and n is the length of the input text. The algorithm works by evaluating lookaround assertions in a bottom-up manner. Our algorithm makes use of a new notion of nondeterministic finite automata (NFAs), which we call oracle-NFAs. These automata are augmented with epsilon-transitions that are guarded by oracle queries that provide the truth values of lookaround assertions at every position in the text. We provide an implementation of our algorithm that incorporates three performance optimizations for reducing the work performed and memory used. We present an experimental comparison against PCRE and Java’s regex library, which are state-of-the-art regex engines that support lookaround assertions. Our experimental results show that, in contrast to PCRE and Java, our implementation does not suffer from super-linear running time and is several times faster.

链接与引用

DOI 原文 · PDF(开放获取) ·

BibTeX
@article{paperbot2636,
  title = {Efficient Matching of Regular Expressions with Lookaround Assertions},
  author = {Konstantinos Mamouras and Agnishom Chattopadhyay},
  journal = {Proceedings of the ACM on Programming Languages},
  volume = {8},
  number = {POPL},
  year = {2024},
  doi = {10.1145/3632934}
}