0

文字列から "Child 1" と "Parent 1" (アポストロフィーなし) を抽出したい

there is a child object with name "Child 1" under parent "Parent 1" in the tree

パタパタ弦

there is a child object with name "([\w\s^"]+)" under parent "([\w\s^"]+)" in the tree

不要な文字列全体にも一致するため、正しくないようです。

http://www.myregextester.com/index.phpでテストしました。

C# で SpecFlow のステップを作成するには、これが必要です。

ありがとう。

4

3 に答える 3

0

私にとっては、これに Regex を使用しない方がすっきりしているように感じます。要件を少し緩めて正規表現のみを試すと、終了引用符と開始引用符の間のテキストが一致します。

おそらく、手動で行うとより良い結果が得られるでしょうか?

    string[] extractBetweenQuotes(string str)
    {
        var list = new List<string>();
        int firstQuote = 0;
        firstQuote = str.IndexOf("\"");

        while (firstQuote > -1)
        {
            int secondQuote = str.IndexOf("\"", firstQuote + 1);
            if (secondQuote > -1)
            {
                list.Add(str.Substring(firstQuote + 1, secondQuote - (firstQuote + 1)));
                firstQuote = str.IndexOf("\"", secondQuote + 1);
                continue;
            }

            firstQuote = str.IndexOf("\"", firstQuote + 1);
        }

        return list.ToArray();
    }

使用法:

string str = "there is a child object with name \"Child 1\" under parent \"Parent 1\" in the tree";

string[] parts = extractBetweenQuotes(str); // Child 1 and Parent 1 (no quotes)
于 2013-09-30T11:21:52.210 に答える