我一直在搜索,但就是找不到解决方案。
我需要使用C#中的正则表达式删除XML文档中标记内的点号。
例如:
test <12.34.56>test.test<12.34>
should be:
test <12346>test.test<1234>
所以基本上是在标签中删除点,有什么想法吗?
resultString = Regex.Replace(subjectString, @"\.(?=[^<>]*>)", "");
仅在下一个紧随其后的尖括号是闭合角括号时,将点替换为空字符串。
当然,这种方法很脆弱,因为闭合角括号可能出现在标签之间的文本中,但如果您确定不会出现这种情况,那么应该没问题。
说明:
\. # Match a dot
(?= # only if the following regex can be matched at the current position:
[^<>]* # - zero or more characters except < or >
> # - followed by a >
) # End of lookahead assertion
XDocument xdoc = XDocument.Load(new StringReader("<root><s123.45><s678.9>aaaa</s678.9></s123.45></root>"));
foreach (var elem in xdoc.Descendants())
elem.Name = elem.Name.LocalName.Replace(".", "");
Console.WriteLine(xdoc);