Irakli Lekishvili
Irakli Lekishvili

Reputation: 34158

Regex get name from string c#

hello everybody how i can get NGG - Acid

from this string

"????log L 06/11/2012 - 06:45:28: \"NGG - Acid<5><STEAM_ID_PENDING><>\" connected, address \"46.49.70.30:27005\"\n\0"

I have tried this

int start = Data.ToString().IndexOf('"') + 1;
            int end = Data.ToString().IndexOf('<');
            return Data.ToString().Substring(start, end - start);

This does not works when username contains space or symbols

Thank you

Update this code gets username but is there better solution:

if (Data.EndsWith("\"")) Data = Data.Substring(0, Data.Length - 1);
            int start = Data.IndexOf("\"");
            int end = Data.IndexOf("<");
            var val = Data.Substring(start + 1, end - 1 - start);

Upvotes: 0

Views: 113

Answers (2)

Kh&#244;i
Kh&#244;i

Reputation: 2143

If you want to use a regex:

Match match = Regex.Match(Data, @"\\""(.+?)<\d+?><ST");

if (match.Success) {
    Console.WriteLine(match.Groups[1].Value);
}

Upvotes: 1

buckley
buckley

Reputation: 14079

Your match will be in group 1 of

\\"(.*?)<

If we suppose your desired match is between \" and <

In C# this becomes

var c = Regex.Match(@"""????log L 06/11/2012 - 06:45:28: \""NGG - Acid<5><STEAM_ID_PENDING><>\"" connected, address \""46.49.70.30:27005\\""\n\0"""
, @"\\""(.*?)<").Groups[1].Value;

Upvotes: 1

Related Questions