我只想去掉XML元素标记中的':‘,在C#中使用正则表达式。
我知道解析文档是可行的,而不是regex..but,它是一个遗留项目,它使用正则表达式来替换XML文档内容。这不是处理XML文档的理想方法,但我无能为力。
我不擅长使用正则表达式,就是想不出只从元素标签而不是值来替换':‘的方法。
例如<tag:name> the value with the tag http://www.example.com </tag:name>
我想将:替换为元素名称中的_ only,而不是value。所以结果应该是:
<tag_name> the value with the tag http://www.example.com </tag_name>
有什么想法吗?
谢谢!
发布于 2011-12-08 03:00:50
这对你有效吗?
Regex tagRegex = new Regex("<[^>]+>");
yourXML = tagRegex.Replace(yourXML, delegate(Match thisMatch)
{
return thisMatch.Value.Replace(":", "_");
});
发布于 2011-12-08 02:24:24
这根针应该能做你想做的事:
<[^>]*(:)[^>]*>
第一个模式组将在标记名称中包含(:)。如果你想做一个替换,你可以用$1_$3
替换(<[^>]*)(:)([^>]*>)
,其中$1
和$3
是子模式。
https://stackoverflow.com/questions/8425338
复制相似问题