我有一個字符串,我需要執行多個搜索並替換以刪除屬性內的前導和尾隨空格。在之前和之後的效果如下所示(視覺和使用它的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