非常坚持正则表达式



在字符串中:

<ut Type="start" Style="external" RightEdge="angle" DisplayText="P id=&quot;2&quot;">&lt;tr&gt;&lt;td width="10%" bgcolor="#C0C0C0" valign="top"&gt;&lt;p align="right"&gt;2&lt;/td&gt;&lt;td width="90%"&gt;</ut><Tu MatchPercent="100"><Tuv Lang="EN-US"><ut Type="start" RightEdge="angle" DisplayText="csf style=&quot;Italic CH&quot; italic=&quot;on&quot;">&lt;!-- 1 --&gt;&lt;FONT COLOR="#FF0000"&gt;&amp;lt;csf style=&quot;Italic CH&quot; italic=&quot;on&quot;&amp;gt;&lt;/FONT&gt;</ut>Battlefield™ V<ut Type="end" LeftEdge="angle" DisplayText="1">&lt;!-- 1 --&gt;&lt;FONT COLOR="#FF0000"&gt;&amp;lt;/1&amp;gt;&lt;/FONT&gt;</ut> (Xbox One)</Tuv><Tuv Lang="NL-NL"><ut Type="start" RightEdge="angle" DisplayText="csf style=&quot;Italic CH&quot; italic=&quot;on&quot;">&lt;!-- 1 --&gt;&lt;FONT COLOR="#FF0000"&gt;&amp;lt;csf style=&quot;Italic CH&quot; italic=&quot;on&quot;&amp;gt;&lt;/FONT&gt;</ut>Battlefield™ V<ut Type="end" LeftEdge="angle" DisplayText="1">&lt;!-- 1 --&gt;&lt;FONT COLOR="#FF0000"&gt;&amp;lt;/1&amp;gt;&lt;/FONT&gt;</ut> (Xbox One)</Tuv></Tu><ut Type="end" Style="external" LeftEdge="angle" DisplayText="P">&lt;/td&gt;&lt;/tr&gt;</ut>`

我想用&amp;quot;替换&quot;

仅当字符串被 FONT 标记包围时,才应发生这种情况,就像在本例中一样。

我正在使用 PHP:

$postproc = preg_replace('#(FONT|G(?!A))((?!/FONT).*?)&quot;(?!/FONT)#', '$1$2&amp;quot;', $postproc);

但是,这不起作用。

这里我们有一个类似的情况:

$postproc = preg_replace('#(DisplayText="|G(?!A))([^">]*)"(?!s*>)#', '$1$2&quot;', $postproc);

这将 DisplayText 标签中的所有 " 引号替换为$quot;主要区别在于 DisplayText 标签以一个字符 ("( 结尾,而上面的 FONT 标签以一系列多个字符结尾,因此我需要负前瞻而不是简单的[^">]否定。

我真的试过了。准确地说是八个小时。我被卡住了。

$postproc用于包含各种标签的整个文件,其中如上所述的多个FONT和DisplayText标签,每个标签可以包含多个替换。

您可以使用

(?:G(?!A)|FONT)
(?:(?!FONT).)+?K
(?<!&amp;)&quot;

这需要用&amp;&quot;替换,看regex101.com 上的演示


细分如下:
(?:G(?!A)|FONT) # match FONT or at the end of the last match
(?:(?!FONT).)+?K # match everything that comes lazily
# do not overrun FONT, forget what has been matched
# thus far (K)
(?<!&amp;)&quot;  # match &quot; only when it is not preceeded by &amp;


更好的是:这个字符串从何而来?你能操纵起源吗?此外,上述答案不适用于嵌套FONT"标签"。

不过这行得通!

$postproc = preg_replace('#(?:G(?!A)|&lt;FONT)(?:(?!FONT).)+?K(?<!&amp;)&quot;#', '$1$2&amp;quot;', $postproc);

这是第一个非捕获组中的额外&lt;

最新更新