gpt4 book ai didi

c# - 正则表达式仅替换给定 xml 节点中的子字符串

转载 作者:行者123 更新时间:2023-11-30 20:56:54 26 4
gpt4 key购买 nike

我徒劳地尝试编写一个正则表达式,如果在 XML 节点中找到给定的子字符串“/oem/en”(后跟正斜杠或双引号),它只会返回匹配项单行 XLIFF 代码。到目前为止,这是我得到的,但它仍然匹配 节点中出现的事件:

/oem/en(?=/|\")(?=.*?</target>)

带有 节点的 XLIFF 代码示例:

<?xml version="1.0" encoding="utf-8"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"><file product-version="1.4" original="" source-language="default" target-language="default" datatype="plaintext" category="page_content"><header></header><body><trans-unit id="page_description" datatype="x-System.String" maxwidth="0" minwidth="0"><source><![CDATA[bla bla]]></source><target><![CDATA[translated bla bla]]></target></trans-unit><trans-unit id="f55c4d88-1f2e-4ad9-aaa8-819af4ee7ee8" datatype="x-System.String" resname="PublishingPageContent" maxwidth="0" minwidth="0"><source><![CDATA[<a href="/oem/en/link1">bla bla</a>]]></source><target><![CDATA[<a href="/oem/en/link1/>translated bla bla</a>]]></target></trans-unit><trans-unit id="f55c4d88-1f2e-4ad9-aaa8-819af4ee7ee8" datatype="x-System.String" resname="PublishingPageContent" maxwidth="0" minwidth="0"><source><![CDATA[<a href="/oem/en/link2">bla bla</a>]]></source><target><![CDATA[<a href="/oem/en/link2/>translated bla bla</a>]]></target></trans-unit></body></file></xliff>

我的方法是尝试制作一个表达式,它会向前看,直到它匹配 或 ,如果它首先找到前者,则意味着我们在 节点中,因此不匹配。

非常感谢您的帮助!

最佳答案

描述

这个表达式只会找到 /oem/en string 如果它在目标标签内

正则表达式:(<target>(?:(?!<\/target>).)*?)(\/oem\/en(?=\/|\"))

替换为:$1~~~~New Value~~~~~

enter image description here

C# 代码示例

输入文本

<?xml version="1.0" encoding="utf-8"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"><file product-version="1.4" original="" source-language="default" target-language="default" datatype="plaintext" category="page_content"><header></header><body><trans-unit id="page_description" datatype="x-System.String" maxwidth="0" minwidth="0"><source><![CDATA[bla bla]]></source><target><![CDATA[translated bla bla]]></target></trans-unit><trans-unit id="f55c4d88-1f2e-4ad9-aaa8-819af4ee7ee8" datatype="x-System.String" resname="PublishingPageContent" maxwidth="0" minwidth="0"><source><![CDATA[<a href="/oem/en/link1">bla bla</a>]]></source><target><![CDATA[<a href="/oem/en/link1/>translated bla bla</a>]]></target></trans-unit><trans-unit id="f55c4d88-1f2e-4ad9-aaa8-819af4ee7ee8" datatype="x-System.String" resname="PublishingPageContent" maxwidth="0" minwidth="0"><source><![CDATA[<a href="/oem/en/link2">bla bla</a>]]></source><target><![CDATA[<a href="/oem/en/link2/>translated bla bla</a>]]></target></trans-unit></body></file></xliff>

代码

using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Class1
{
static void Main(string[] args)
{
String sourcestring = "source string to match with pattern";
String matchpattern = @"(<target>(?:(?!<\/target>).)*?)(\/oem\/en(?=\/|\""))";
String replacementpattern = @"$1~~~~~new value~~~~~";
Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern,RegexOptions.IgnoreCase));
}
}
}

产量

<?xml version="1.0" encoding="utf-8"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"><file product-version="1.4" original="" source-language="default" target-language="default" datatype="plaintext" category="page_content"><header></header><body><trans-unit id="page_description" datatype="x-System.String" maxwidth="0" minwidth="0"><source><![CDATA[bla bla]]></source><target><![CDATA[translated bla bla]]></target></trans-unit><trans-unit id="f55c4d88-1f2e-4ad9-aaa8-819af4ee7ee8" datatype="x-System.String" resname="PublishingPageContent" maxwidth="0" minwidth="0"><source><![CDATA[<a href="/oem/en/link1">bla bla</a>]]></source><target><![CDATA[<a href="~~~~~new value~~~~~/link1/>translated bla bla</a>]]></target></trans-unit><trans-unit id="f55c4d88-1f2e-4ad9-aaa8-819af4ee7ee8" datatype="x-System.String" resname="PublishingPageContent" maxwidth="0" minwidth="0"><source><![CDATA[<a href="/oem/en/link2">bla bla</a>]]></source><target><![CDATA[<a href="~~~~~new value~~~~~/link2/>translated bla bla</a>]]></target></trans-unit></body></file></xliff>

关于c# - 正则表达式仅替换给定 xml 节点中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17236183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com