在字符串中:
<ut Type="start" Style="external" RightEdge="angle" DisplayText="P id="2""><tr><td width="10%" bgcolor="#C0C0C0" valign="top"><p align="right">2</td><td width="90%"></ut><Tu MatchPercent="100"><Tuv Lang="EN-US"><ut Type="start" RightEdge="angle" DisplayText="csf style="Italic CH" italic="on""><!-- 1 --><FONT COLOR="#FF0000">&lt;csf style="Italic CH" italic="on"&gt;</FONT></ut>Battlefield™ V<ut Type="end" LeftEdge="angle" DisplayText="1"><!-- 1 --><FONT COLOR="#FF0000">&lt;/1&gt;</FONT></ut> (Xbox One)</Tuv><Tuv Lang="NL-NL"><ut Type="start" RightEdge="angle" DisplayText="csf style="Italic CH" italic="on""><!-- 1 --><FONT COLOR="#FF0000">&lt;csf style="Italic CH" italic="on"&gt;</FONT></ut>Battlefield™ V<ut Type="end" LeftEdge="angle" DisplayText="1"><!-- 1 --><FONT COLOR="#FF0000">&lt;/1&gt;</FONT></ut> (Xbox One)</Tuv></Tu><ut Type="end" Style="external" LeftEdge="angle" DisplayText="P"></td></tr></ut>`
我想用&quot;
替换"
仅当字符串被 FONT 标记包围时,才应发生这种情况,就像在本例中一样。
我正在使用 PHP:
$postproc = preg_replace('#(FONT|G(?!A))((?!/FONT).*?)"(?!/FONT)#', '$1$2&quot;', $postproc);
但是,这不起作用。
这里我们有一个类似的情况:
$postproc = preg_replace('#(DisplayText="|G(?!A))([^">]*)"(?!s*>)#', '$1$2"', $postproc);
这将 DisplayText 标签中的所有 " 引号替换为$quot;
主要区别在于 DisplayText 标签以一个字符 ("( 结尾,而上面的 FONT 标签以一系列多个字符结尾,因此我需要负前瞻而不是简单的[^">]
否定。
我真的试过了。准确地说是八个小时。我被卡住了。
$postproc用于包含各种标签的整个文件,其中如上所述的多个FONT和DisplayText标签,每个标签可以包含多个替换。
您可以使用
(?:G(?!A)|FONT)
(?:(?!FONT).)+?K
(?<!&)"
这需要用&"
替换,看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)
(?<!&)" # match " only when it is not preceeded by &
更好的是:这个字符串从何而来?你能操纵起源吗?此外,上述答案不适用于嵌套
FONT
"标签"。
不过这行得通!
$postproc = preg_replace('#(?:G(?!A)|<FONT)(?:(?!FONT).)+?K(?<!&)"#', '$1$2&quot;', $postproc);
这是第一个非捕获组中的额外<