2013-10-17 51 views
1

我正在處理一個正則表達式,我只是無法弄清楚問題所在。我已經嘗試了幾種幫助網站如http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashxhttp://gskinner.com/RegExr/但不知何故,當我把在C#中測試正則表達式沒有正確正則表達式組替換

我工作的一個JSON字符串,我可以收到來自JIRA處理。這個JSON字符串的大量精簡,美化版本如下:

{ 
    "fields": { 
     "progress": { 
      "progress": 0, 
      "total": 0 
     }, 
     "summary": "Webhook listener is working", 
     "timetracking": {}, 
     "resolution": null, 
     "resolutiondate": null, 
     "timespent": null, 
     "reporter": { 
      "self": "http://removed.com/rest/api/2/user?username=removed", 
      "name": "[email protected]", 
      "emailAddress": "[email protected]", 
      "avatarUrls": { 
       "16x16": "http://www.gravatar.com/avatar/88994b13ab4916972ff1861f9cccd4ed?d=mm&s=16", 
       "24x24": "http://www.gravatar.com/avatar/88994b13ab4916972ff1861f9cccd4ed?d=mm&s=24", 
       "32x32": "http://www.gravatar.com/avatar/88994b13ab4916972ff1861f9cccd4ed?d=mm&s=32", 
       "48x48": "http://www.gravatar.com/avatar/88994b13ab4916972ff1861f9cccd4ed?d=mm&s=48" 
      }, 
      "displayName": "Wubinator]", 
      "active": true 
     }, 
     "updated": "2013-08-20T14:08:00.247+0200", 
     "created": "2013-07-30T14:41:07.090+0200", 
     "description": "Say what?", 
     "customfield_10001": null, 
     "duedate": null, 
     "issuelinks": [], 
     "customfield_10004": "73", 
     "worklog": { 
      "startAt": 0, 
      "maxResults": 0, 
      "total": 0, 
      "worklogs": [] 
     }, 
     "project": { 
      "self": "http://removed.com/rest/api/2/project/EP", 
      "id": "10000", 
      "key": "EP", 
      "name": "EuroPort+ Suite", 
      "avatarUrls": { 
       "16x16": "http://removed.com/secure/projectavatar?size=xsmall&pid=10000&avatarId=10208", 
       "24x24": "http://removed.com/secure/projectavatar?size=small&pid=10000&avatarId=10208", 
       "32x32": "http://removed.com/secure/projectavatar?size=medium&pid=10000&avatarId=10208", 
       "48x48": "http://removed.com/secure/projectavatar?pid=10000&avatarId=10208" 
      } 
     }, 
     "customfield_10700": null, 
     "timeestimate": null, 
     "lastViewed": null, 
     "timeoriginalestimate": null, 
     "customfield_10802": null 
    } 
} 

我需要這個JSON轉換爲XML,當然這是因爲「16×16」,「24×24」的不能直接「 32x32「和」48x48「位,它們將被轉換爲< 16x16 />,< 24x24 />,< 32x32 />和< 48x48 />這些標籤是無效標籤。

XML的接收者甚至不需要那些頭像網址,所以我正在考慮剝離出整個「avatarUrls」:「{.....},在將JSON交給JSON.NET之前。轉換

我在想這樣做,使用正則表達式上提到的網站我來到了以下正則表達式經過一些測試:

("avatarUrls)(.*?)("displayName")

的Regex.Replace方法應該刪除所有發現的結果,而不是第三次探訪(又名「顯示名稱」)

網站http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx顯示我正確的學習小組,結果,並說,所提到的正則表達式應內部C#中使用是:

@"(""avatarUrls)(.*?)(""displayName"")"

所以內部C#我寫了下面:

string expression = @"(""avatarUrls)(.*?)(""displayName"")"; 
string result = Regex.Replace(json, expression, "$3"); 

return result; 

當在RegexReplace沒有被替換之後,我看着結果。有人看到我在這裏做錯了嗎?

+0

你有沒有考慮XML轉義無效的標籤名稱並進行直轉換成XML? http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx – Gusdor

+0

溝這種方式,只不過抽絲你一些[json.net](http://james.newtonking.com/json)。有一個JSON => XML轉換準備好你去。 – Will

+0

我將通過運行Regex.Match並檢查組和捕獲來調試它。確保第三組真的包含顯示名稱等,然後從那裏開始。 – Baldrick

回答

0

有一個Regex.Replace超載需要RegexOptions,你可能需要看看。例如,要使.匹配每個字符(而不是除\ n之外的每個字符),則需要指定RegexOptions.Singleline。此外,它看起來像你試圖取代@"(""avatarUrls)(.*?)(""displayName"")"$3每一場比賽是打算?你可能會更好的做這樣的事情:

var match = Regex.Match(json, pattern, options); 
while (match.Success) { 
     // Do stuff with match.Groups(1) 
     match = match.NextMatch(); 
} 

但是......我不太確定那會在源字符串中替換它。

0

的問題是完全不同的東西:

裏面以下字符串:

{"16x16":"http://www.gravatar.com/avatar/88994b13ab4916972ff1861f9cccd4ed?d=mm&s=16, "32.32" 

有一個「&」魔法符號,其指示下一個參數開始。因此,沒有完整的JSON被讀取,因此它不能正確地轉換它。它還指出爲什麼在我使用的正則表達式內沒有任何內容被替換,因爲「displayName」不在字符串內部,所以沒有任何匹配。

1

我不會使用正則表達式來刪除這些節點。我會使用JSON。Net去除你不想要的節點。

我指的是quote

Some people, when confronted with a problem, think 「I know, I'll use regular expressions.」 Now they have two problems.

使用答案找到here,你可以寫:

var jsonObject = (JObject)JsonConvert.DeserializeObject(yourJsonString); 
removeFields(jsonObject.Root, new[]{"avatarUrls"}); 

(請注意,我不知道,如果你想刪除這兩個 「avatarUrls」節點)。

+0

我會看看,看起來像乾淨的解決方案 – Wubinator

+0

@Wubinator你使用JSON .Net來解決這個問題嗎? –