0

次のような文字列があります。

Addadafafa/DHello/p2324141142DsddDsdsds/Dgood/p23323

気付かなかった人のために、私が保持したいテキストは常に と の間に/Dあり/pます。正規表現を使用して解析しようとしましたが、すべての文字列に対して解析できませんでした。常に最初または最後の単語を保持します。

/D前の文字列の間と元のすべての単語を含む新しい文字列を保持するにはどうすればよい/pですか?

期待される出力:

hello good
4

3 に答える 3

6
string input = "Addadafafa/DHello/p2324141142DsddDsdsds/Dgood/p23323";
var parts = Regex.Matches(input, "/D(.+?)/p")
                 .Cast<Match>()
                 .Select(m => m.Groups[1].Value)
                 .ToList();

string finalStr = String.Join(" ", parts); //If you need this.
于 2012-11-01T22:53:23.787 に答える
1

これを試して:

    string str = "Addadafafa/DHello/p2324141142DsddDsdsds/Dgood/p23323";
    Regex reg = new Regex(@"/D(\w+)/p");
    MatchCollection matches = reg.Matches(str);
    string result = "";
    foreach (Match match in matches)
    {
        result += match.Result("$1") + " ";
    }
    Console.WriteLine(result);  

または:

    string str = "Addadafafa/DHello/p2324141142DsddDsdsds/Dgood/p23323";
    Regex reg = new Regex(@"(?!/D)[^D]\w+(?=/p)");
    MatchCollection matches = reg.Matches(str);
    string result = "";
    foreach (Match match in matches)
    {
        result += match.Value + " ";
    }
    Console.WriteLine(result);
于 2012-11-02T02:00:59.063 に答える
1
var result = input.Split(new[] {"/D", "/p"}, 
                              StringSplitOptions.RemoveEmptyEntries)
                  .Where((w, i) => (i & 1) == 1);
于 2012-11-02T03:07:34.507 に答える