我有一个字符串,我需要执行多个搜索并替换以删除属性内的前导和尾随空格。在之前和之后的效果如下所示(视觉和使用它的JS例子):正则表达式帮助 - 从JavaScript转换为C#
现在,我需要做的在C#中的等价 - 替换字符串中的所有引用。但我很困难。我知道这个模式是正确的,如图中的JS版本,但语法/转义语法做我的头英寸
这里是我的,但当然这是行不通的;-)
//define the string
string xmlString = "<xml><elementName specificattribute=" 111 222 333333 " anotherattribute="something" somethingelse="winkle"><someotherelement>value of some kind</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName></xml>";
// here's the regExPattern - the syntax checker doesn't like this at all
string regExPattern = "/(specificattribute=)"\s*([^"]+?)\s*"/g";
// here's the replacement
string replacement = "$1\"$2\"";
Regex rgx = new Regex(regExPattern);
string result = rgx.Replace(xmlString, replacement);
有人能告诉我我的方式错误吗?
非常感谢!
尝试把一个@符号从像这样的regExPattern字符串: 串regExPattern = @ “/(specificattribute =)” \ s *(?[^“] +) \ s *“/ g”; – 2010-02-04 23:33:04
您不应该使用正则表达式来解析XML.C#拥有强大的XML文档处理工具 – 2010-02-04 23:36:03