0

Im getting strings somethink like this

"????log L 07/13/2012 - 23:59:21: \"Baobi<1><STEAM_ID_PENDING><>\" connected, address \"72.539.185.197:27005\"\n\0"

I had this method but sometimes its very buggy

 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);
                return val;

Please provides some good regex to get Baobi from here

4
  • 4
    Your current solution looks perfectly right to me. Why do you want a regex? Commented Jul 13, 2012 at 20:08
  • Good point, @Gene. OP: If you show some examples of what strings aren't working with your current code, we can be sure to have the regex take care of that. Commented Jul 13, 2012 at 20:09
  • I dont know string where occured error. Only i know error message from Windows Server Event viewer Commented Jul 13, 2012 at 20:12
  • Can't you see anything else about the error, e.g. to determine if Data was null or if an exception was thrown from Substring or IndexOf? You might want to put a try/catch around it and throw or log a more descriptive message that includes the contents of the string. Commented Jul 13, 2012 at 20:14

1 Answer 1

2

AFAIK this should work:

return Regex.Match(Data, @"""(.+?)<").Groups[1].ToString();

If this is still buggy, please give examples of strings that are problematic, and the results (e.g. exception details or what string was actually returned).

Sign up to request clarification or add additional context in comments.

1 Comment

Or similarly, return Regex.Match(Data, @"""(.+?)<").Groups[1].ToString();. That has the advantage of allowing for more characters there (such as numbers).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.