2010-02-04 85 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(); 
    }