[英]Regular expression groups replacement
我正在使用正则表达式,但我只是想不出问题所在。 我已经尝试了多个帮助网站,例如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的接收器甚至不需要这些头像网址,因此我正在考虑剥离整个“ avatarUrls”:“ {.....},然后再将JSON交给JSON.NET进行转换。
我正在考虑使用正则表达式进行此操作。 在上述网站上进行测试后,我得出以下正则表达式:
( “avatarUrls)(。*?)(” 显示名“)
Regex.Replace方法应该删除所有找到的结果,而不是第三个抱怨(aka“ displayName”)
网站http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx向我显示了正确的组并找到结果,并说应在内部使用提及的正则表达式C#是:
@ “(” “avatarUrls)(。*?)(” “显示名” “)”
因此,在C#中,我编写了以下代码:
string expression = @"(""avatarUrls)(.*?)(""displayName"")";
string result = Regex.Replace(json, expression, "$3");
return result;
当我查看RegexReplace之后的结果时,什么都没有被替换。 有人在这里看到我做错了吗?
Regex.Replace
的重载带有您可能需要研究的RegexOptions 。 例如, .
要匹配每个字符(而不是\\ n以外的每个字符),您需要指定RegexOptions.Singleline
。 另外,您似乎要用$3
替换@"(""avatarUrls)(.*?)(""displayName"")"
所有匹配项? 您最好这样做:
var match = Regex.Match(json, pattern, options);
while (match.Success) {
// Do stuff with match.Groups(1)
match = match.NextMatch();
}
但是...我不太确定会在源字符串中替换它。
问题是完全不同的:
在以下字符串中:
{"16x16":"http://www.gravatar.com/avatar/88994b13ab4916972ff1861f9cccd4ed?d=mm&s=16, "32.32"
魔术符号“&”表示下一个参数已启动。 因此,无法读取完整的JSON,因此无法正确转换。 它还表明为什么在我使用的正则表达式内什么都不能替换,因为“ displayName”不在字符串内,所以没有匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.