私は正規表現に取り組んでいますが、何が問題なのかわかりません。http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashxやhttp://gskinner.com/RegExr/のようないくつかの支援サイトを試しましたが、どういうわけかテストした正規表現をC#に入れると、正しく処理されません
JIRA から受け取ることができる JSON 文字列に取り組んでいます。この 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": "removed@nothere.com",
"emailAddress": "removed@nothere.com",
"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 に変換する必要があります。これは、json 内の「16x16」、「24x24」、「32x32」、および「48x48」ビットが <16x16 />、<24x24 に変換されるため、直接行うことはできません。 />、<32x32 />、および <48x48 /> タグは無効なタグです。
XML の受信者はこれらのアバター URL を必要としないので、"avatarUrls":"{ ..... } 全体を取り除くことを考えていたので、変換のために json を JSON.NET に渡す少し前に考えていました。
正規表現を使ってこれを行うことを考えていました。上記のWebサイトでいくつかのテストを行った後、次の正規表現にたどり着きました。
("avatarUrls)(.*?)("displayName")
Regex.Replace メソッドは、3 番目のグループ (別名「displayName」) ではなく、見つかったすべての結果を削除する必要があります。
ウェブサイト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 の後に結果を見ると、何も置き換えられていません。ここで私が間違ったことを誰かが見ていますか?