我正在研究正则表达式,但我无法弄清楚问题所在。我尝试了几个帮助网站,例如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 进行转换。
我正在考虑使用正则表达式来做到这一点。在上述网站上进行了一些测试后,我得出了以下正则表达式:
("avatarUrls)(.*?)("displayName")
Regex.Replace 方法应该删除所有找到的结果,而不是第三个 groep(又名“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 之后查看结果时,没有任何内容被替换。有谁看到我在这里做错了什么?