tags:

views:

46

answers:

3

I need a regex pattern which will accommodate for the following.

I get a response from a UDP server, it's a very long string and each word is separated by \, for example:

\g79g97\g879o\wot87gord\player_0\name0\g6868o\g78og89\g79g79\player_1\name1\gyuvui\yivyil\player_2\name2\g7g87\g67og9o\v78v9i7

I need the strings after \player_n\, so in the above example I would need name0, name1 and name3,

I know this is the second regex question of the day but I have the book (Mastering Regular Expressions) on order! Thank you.

UPDATE. elusive's regex pattern will suffice, and I can add the match(0) to a textbox. However, what if I want to add all the matches to the text box ?

textBox1.Text += match.Captures[0].ToString(); //this works fine.

How do I add "all" match.captures to the text box? :s sorry for being so lame, this Regex class is brand new to me .

+3  A: 

Try this one:

\\player_\d+\\([^\\]+)
elusive
+1 -- Hint: for regular expressions, it's best to post the pure expression without language specific additions. Like in this case, the regex literal notation `/slashes/` are not meaningful in C#.
Tomalak
Thanks for your addition! I used them in Perl for my entire life, so delimiters seem quite natural to me.
elusive
it didnt work, but if i remove the "/" from each end, i get a match "\player_n\playername\" .. which is ok, but i would prefer just "playername" by itself
brux
You need to use the first match group. I do not know how this works in C#, but this is not too difficult in any other language that knows about regular expressions.
elusive
@brux: See http://www.regular-expressions.info/dotnet.html to get a quick overview (how to handle groups is explained there as well).
Tomalak
var match = Regex.Match(stringData, @"\\player_\d+\\([^\\]+)");textBox1.Text += match.Captures[0].ToString();textBox1.Text += match.Captures[1].ToString(); it crashes when adding the second match to the textbox. if i remove this instruction then the first match (0) gets added fine.
brux
its as though its oly finding 1 match, but there are plenty of player names in the string :s
brux
+1  A: 

To get only the player name, you could use:

(?<=\\player_\d+\\)[^\\]+

This (?<=\\player_\d+\\) is something called a positive look-behind. It makes sure that the actual match [^\\]+ is preceded by the expression in the parentheses.

In this case, it's even specific to only a few regex engines (.NET being among them, luckily), in that it contains a variable length expression (due to \d+). Most regex engines only support fixed-length look-behind.

In any case, look-behind is not necessarily the best approach to this problem, match groups are simpler easier to read.

Tomalak
hi tomolak, im going with the regex posted by elusive since it works ok. but if i try and add the second match to the textbox i get an "out of range error". if i remove that line the first match (0) is added ok. in the string i compare to the pattern, there are about 8 playernames :s
brux
@brux: If you get "out of range" errors you are doing it wrong anyway. Always do range checks in your code.
Tomalak
+1  A: 

i think that this test sample can help you

string inp = @"\g79g97\g879o\wot87gord\player_0\name0\g6868o\g78og89\g79g79\player_1\name1\gyuvui\yivyil\player_2\name2\g7g87\g67og9o\v78v9i7";
string rex = @"[\w]*[\\]player_[0-9]+[\\](?<name>[A-Za-z0-9]*)\b";
Regex re = new Regex(rex);
Match mat = re.Match(inp);
 for (Match m = re.Match(inp); m.Success; m = m.NextMatch())
{
     Console.WriteLine(m.Groups["name"]);
}

you can take the name of the player from the m.Groups["name"]

Ghiath Zarzar