首页 > 解决方案 > 避免将正则表达式与行首匹配

问题描述

我想出了以下正则表达式:

(.*\\\\documentclass.*)|(.*\\\\documentstyle.*)

此正则表达式将用于确定文档是否包含 \documentclass 或 \documentstyle 标记。

我想避免匹配以%这样开头的字符串:

% To use with LaTeX, use \documentstyle[psfig,...]{...}

我怎样才能包含某种负面的lookbehind以避免匹配这个字符串?

标签: pythonregex

解决方案


使用(使用多行模式)锚定到行^首,负前瞻%,然后交替:

(?m)^(?!%)(?:.*\\document(?:class|style).*)

https://regex101.com/r/WjCkfb/1


推荐阅读