我正在處理一個正則表達式,我只是無法弄清楚問題所在。我已經嘗試了幾種幫助網站如http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx和http://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沒有被替換之後,我看着結果。有人看到我在這裏做錯了嗎?
你有沒有考慮XML轉義無效的標籤名稱並進行直轉換成XML? http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx – Gusdor
溝這種方式,只不過抽絲你一些[json.net](http://james.newtonking.com/json)。有一個JSON => XML轉換準備好你去。 – Will
我將通過運行Regex.Match並檢查組和捕獲來調試它。確保第三組真的包含顯示名稱等,然後從那裏開始。 – Baldrick