使用正则表达式附加带有附加文本的 XML 元素的值

Append value of an XML Element with additional text using regular expressions

我正在尝试将以下内容转换为 XML

<SharedDataSetReference>AnyDataSetName</SharedDataSetReference>

<SharedDataSetReference>/DataSets/AnyDataSetName</SharedDataSetReference>

同时,

的现有实例
<SharedDataSetReference>/DataSets/AnyDataSetName</SharedDataSetReference>

应该保持不变。

请注意 AnyDataSetName 可以有

<SharedDataSetReference>Company</SharedDataSetReference>
<SharedDataSetReference>Customer</SharedDataSetReference>

或 /Datasets/Order

他们应该改成

<SharedDataSetReference>/Datasets/Company</SharedDataSetReference>
<SharedDataSetReference>/Datasets/Customer</SharedDataSetReference>
<SharedDataSetReference>/Datasets/Order</SharedDataSetReference>

试试这个

<SharedDataSetReference>([^\/]+)<\/SharedDataSetReference>

https://regex101.com/r/yM7tA0/1

谢谢 Tim007 这就是我所做的

var sharedDatasets = Regex.Replace(datasets.First().ToString(), String.Format(@"<{0}>(?<DataSetReferenceValue>[^\/]+)<\/{0}>", "SharedDataSetReference"), delegate(Match match)
            {
                var value = String.Concat("/Datasets/", match.Groups["DataSetReferenceValue"].ToString());
                return String.Format(@"<{0}>{1}</{0}>", "SharedDataSetReference", value);
            }, RegexOptions.Multiline
            )