使用preg_replace时出现的问题

huangapple go评论57阅读模式
英文:

Problems using preg_replace

问题

Regx不是我的强项。

我有一个大文件,我想要替换以下示例:

<g:id><![CDATA[131614-3XL]]></g:id>

应该替换为:

<g:id><![CDATA[131614-3XL]]></g:id><g:id2><![CDATA[131614]]></g:id2>

请注意," -3XL" 在id2中被删除,并请注意,-3XL可能是许多其他组合。例如:-4XL或-32/32或-42.5等等。但它总是以" -"开头。

我尝试使用preg_replace,但我无法弄清楚。

英文:

Regx is not my thing.

I have a large file where I want to replace the following example:

&lt;g:id&gt;&lt;![CDATA[131614-3XL]]&gt;&lt;/g:id&gt;

should be replace with:

&lt;g:id&gt;&lt;![CDATA[131614-3XL]]&gt;&lt;/g:id&gt;&lt;g:id2&gt;&lt;![CDATA[131614]]&gt;&lt;/g:id2&gt;

Please note that "-3XL" is deleted in id2 and please note that -3XL could be many other combinations. fx. -4XL or -32/32 or -42,5 and so on. But it always starts with -

I have tried using preg_replace but I can figure it out.

答案1

得分: 0

以下是您可以开始使用的代码。根据CDATA内容的更改程度,您可能需要进行一些调整。

$str = "<g:id><![CDATA[131614-3XL]]></g:id>";
$expected = "<g:id><![CDATA[131614-3XL]]></g:id><g:id2><![CDATA[131614]]></g:id2>";

# 获取第一部分,不包括g:id包装和-3XL部分
$post_replace = preg_replace("/^<g:id>(.*?CDATA\[\d+)\-[^\]]+(.*?)<\/g:id>$/","$1$2", $str);

$output = "$str<g:id2>$post_replace</g:id2>";

if ($output == $expected) {
    print "Success\n";
}

请注意,上述代码中的HTML实体(如&quot;)已被替换为正常的HTML标记(如")。

英文:

Here is code that you can start with. You might need to adjust depending upon how much the CDATA content changes.

$str = &quot;&lt;g:id&gt;&lt;![CDATA[131614-3XL]]&gt;&lt;/g:id&gt;&quot;;
$expected = &quot;&lt;g:id&gt;&lt;![CDATA[131614-3XL]]&gt;&lt;/g:id&gt;&lt;g:id2&gt;&lt;![CDATA[131614]]&gt;&lt;/g:id2&gt;&quot;;

# Get the first section without the g:id wrapper and without the -3XL section
$post_replace = preg_replace(&quot;/^&lt;g:id&gt;(.*?CDATA\[\d+)\-[^\]]+(.*?)&lt;\/g:id&gt;$/&quot;,&quot;$1$2&quot;, $str);

$output = &quot;$str&lt;g:id2&gt;$post_replace&lt;/g:id2&gt;&quot;;

if ($output == $expected) {
    print &quot;Success\n&quot;;
}

答案2

得分: 0

使用捕获组来提取 CDATA 中的数字,这样你就可以将它复制到替换部分而不包括 -XXX

$result = preg_replace('##&lt;g:id&gt;&lt;!\[CDATA\[(\d+)-[^]]+\]\]&gt;&lt;/g:id&gt;##', '$0&lt;g:id2&gt;&lt;![CDATA[$1]]&gt;&lt;/g:id2&gt;', $string);

$0 是整个匹配,$1 是 CDATA 中的数字。

英文:

Use a capture group to grab the number in the CDATA so you can copy it to the replacement without the -XXX after it.

$result = preg_replace(&#39;#&lt;g:id&gt;&lt;!\[CDATA\[(\d+)-[^]]+\]\]&gt;&lt;/g:id&gt;#&#39;, &#39;$0&lt;g:id2&gt;&lt;![CDATA[$1]]&gt;&lt;/g:id2&gt;&#39;, $string);

$0 is the entire match, $1 is the number in the CDATA.

DEMO

huangapple
  • 本文由 发表于 2023年3月4日 00:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定