2010-02-04 42 views
4

我有一个字符串,我需要执行多个搜索并替换以删除属性内的前导和尾随空格。在之前和之后的效果如下所示(视觉和使用它的JS例子):正则表达式帮助 - 从JavaScript转换为C#

http://lloydi.com/x/re/

现在,我需要做的在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); 

有人能告诉我我的方式错误吗?

非常感谢!

+0

尝试把一个@符号从像这样的regExPattern字符串: 串regExPattern = @ “/(specificattribute =)” \ s *(?[^“] +) \ s *“/ g”; – 2010-02-04 23:33:04

+3

您不应该使用正则表达式来解析XML.C#拥有强大的XML文档处理工具 – 2010-02-04 23:36:03

回答

2

删除regExPattern末尾的/ g。这是我确定的第一个错误。 .NET的正则表达式实现没有全局修饰符,默认情况下它是全局的。

UPDATE:

我认为这应该工作:

  //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*"; 

      // here's the replacement 
      string replacement = "$1\"$2\""; 

      Regex rgx = new Regex(regExPattern); 
      string result = rgx.Replace(xmlString, replacement); 

虽然这实际上可能为你工作,XML的嵌套/上下文特定的性质使得正则表达式不适合正常,高效地解析它。这当然不是这项工作的最佳工具,让我们这样说吧。

从外观上看,您应该真正使用Xpath或Linq到XML来解析和修改这些属性。

我几乎偷了马克·拜尔的答案,但因为他的例子是使用XML文件,你在内存这样应该更有这样的:

XDocument doc = XDocument.Parse("<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>"); 
foreach (XAttribute attr in doc.Descendants("elementName") 
           .Attributes("specificattribute")) 
{ 
    attr.Value = attr.Value.Trim(); 
} 
string result = doc.ToString(); 
3

不要使用正则表达式这个任务。 .NET拥有用于处理XML文档的强大工具。试试这个:

XDocument doc = XDocument.Load("input.xml"); 
foreach (XAttribute attr in doc.Descendants("elementName") 
           .Attributes("specificattribute")) 
{ 
    attr.Value = attr.Value.Trim(); 
} 
doc.Save("output.xml"); 
+0

+1。我比你更喜欢你的答案。 – 2010-02-05 00:02:32

0

说真的,你应该为此使用System.Xml类。下面是使用XPath另一个例子:

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>"; 

    XmlDocument xml = new XmlDocument(); ; 
    xml.LoadXml(xmlString); 

    foreach (XmlAttribute el in xml.SelectNodes("//@specificattribute")) 
    { 
     el.Value = el.Value.Trim(); 
    }