1

私は正規表現に取り組んでいますが、何が問題なのかわかりません。http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashxhttp://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 の後に結果を見ると、何も置き換えられていません。ここで私が間違ったことを誰かが見ていますか?

4

3 に答える 3

1

これらのノードを削除するために正規表現は使用しません。代わりに JSON .Net を使用して、不要なノードを削除します。

私は引用を参照します:

問題に直面したときに、「分かった、正規表現を使用する」と考える人もいます。現在、彼らには 2 つの問題があります。

ここにある答えを使用して、次のように書くことができます。

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

(両方の「avatarUrls」ノードを削除したいかどうかわからないことに注意してください。)

于 2013-10-17T15:04:15.537 に答える
0

調べる必要があるかもしれないRegexOptionsRegex.Replaceを取るオーバーロードがあります。たとえば、to が (\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();
}  

ただし...ソース文字列でそれが置き換えられるかどうかはわかりません。

于 2013-10-17T13:44:34.890 に答える