Hello Everybody i asked this question few hours ago C# get username from string. split
Now i have difficult problem. Trying to get Acid Player And m249 from this string
L 02/28/2012 - 06:14:22: "Acid<1><VALVE_ID_PENDING><CT>"
killed "Player<2><VALVE_ID_PENDING><TERRORIST>" with "m249"
I tried this
int start = Data.ToString().IndexOf('"') + 1;
int end = Data.ToString().IndexOf('<');
var Killer = Data.ToString().Substring(start, end - start);
int start1 = Data.ToString().IndexOf("killed") + 1;
int end1 = Data.ToString().IndexOf('<') + 4;
var Victim = Data.ToString().Substring(start1, end1 - start1);
but its show this exception on last line
Length cannot be less than zero.
Parameter name: length
Does it possible to get Both player name and last string (m249)
Tanks
Here is a simple example of how you can do it with regex. Depending on how much the string varies, this one may work for you. I’m assuming that quotes (“) are consistent as well as the text between them. You’ll need to add this line at the top:
Code:
The syntax breakdown for the regex is this:
means, go till we hit a double quote (“)
means take the quote as the next part of the string, since the previous term brings us to it, but doesn’t go past it.
The parenthesis means we are interested in the results of this part, we will seek till we hit a less than (<). since this is the first “group” we’re looking to extract, it’s referred to as Groups[1] in the match. Again we have the character we were searching for to consume it and continue our search.
This will again search, without keeping the results due to no parenthesis, till we hit the next quote mark. We then manually specify the string of (” killed “) since we’re interested in what’s after that.
This will capture any characters for our Group[2] result that are alphanumeric, upper or lowercase.
Search and ignore the rest till we hit the next double quote
Another literal string that we’re using as a marker
Same as above, return alphanumeric as our Group[3] with the parenthesis
End it off with the last quote.
Hopefully this explains it. A google for “Regular Expressions Cheat Sheet” is very useful for remembering these rules.