具有负前瞻和点匹配换行修饰符 (/s) 的正则表达式

Regex with negative lookahead and dot matches newline modifier (/s)

我有一个 PHP 脚本,我需要匹配特定字符串的最后一次出现。

假设我有以下场景:

1

<p class="TPTexto" style="text-autospace: none; ">
<font face="Arial" size="2" color="#FF0000">Este texto não substitui o publicado no DOU de  28.9.2006.</font>
</p>

2

Este texto abc def
<p class="TPTexto" style="text-autospace: none; ">
<font face="Arial" size="2" color="#FF0000">Este texto não substitui o publicado no DOU de  28.9.2006.</font>
</p>

3

Este texto abc def
<p class="TPTexto" style="text-autospace: none; ">
<font face="Arial" size="2" color="#FF0000">Este 
texto não substitui o publicado no DOU de  28.9.2006.</font>
</p>

4

Este texto abc def
<p class="TPTexto" style="text-autospace: none; ">
<font face="Arial" size="2" color="#FF0000">Este <font></font>
texto não substitui o publicado no DOU de  28.9.2006.</font>
</p>

5

Este texto abc def
<p class="TPTexto" style="text-autospace: none; ">
<font face="Arial" size="2" color="#FF0000">Este            texto não substitui     o     publicado no DOU de  28.9.2006.</font>
</p>

我想在所有情况下都匹配 Este texto não substitui o publicado,在两者之间接受一些偶尔的垃圾,比如 Este <font></font>\ntexto não substitui o publicado

所以我使用了以下正则表达式:
/Este(?:.(?!Este))+?texto.+?n.+?o.+?substitui.+?o.+?publicado/uis

旗帜:
u 接受 unicode 字符
i 接受不敏感的内容
s 使点 (.) 匹配换行符(所以我的否定前瞻有效)

这样我匹配最后的Este和后面的文字,如我所愿,对吧?没有! s 修饰符杀死它。
(我正在使用 this PHP tool 顺便测试一下)

我不知道为什么 s 修饰符在这种情况下会杀死它。任何帮助将不胜感激。


我在这个项目上使用 PHP 的 preg_match_all

编辑:

注意到不清楚:我需要第二个 Este texto... 而不是第一个。

你的正则表达式没问题。你可以在你的正则表达式前面加上这个:

\A.*\K
  • \A 断言输入字符串的开头
  • .* 立即匹配整个输入字符串,然后尝试回溯以匹配下一个模式,即 Este
  • \K 将输出重置为该点,以便您只会看到所需的字符串

我删除了前瞻并使您的正则表达式更简单一些。把它们放在一起我们有这个:

\A.*\KEste.+?texto.+?n.+?o.+?substitui.+?o.+?publicado