开发者

C# Regex need pattern for string match

开发者 https://www.devze.com 2023-01-15 07:10 出处:网络
This is my second attempt to get this answer since I messed it up first time :s As you can guess Im new to regex

This is my second attempt to get this answer since I messed it up first time :s As you can guess Im new to regex

basically, the string contains lots of words seperated by "开发者_运维百科\"'s. so for example

\gyifgy8i\gyigyi9g\player_0\k1ng*tar%\gp86gg78.\g79g\player_1\th3dadY>\gyigyigiy\huiohgiu\player_2\j0k$r\g68g6o9g\987pgh890\player_3\PLAYERNAME

I need a patter which can match each word after \player_n\ , where n is a number. so from the above i want to match k1ng*tar% and th3dadY> and j0k$r (without the \ either side). these are player names from a UDP query, the udp query sperates every value with "\". every player name is always preceeded by \player_n\ as you can see above.

originally i was advised to used:

string rex = @"[\w]*[\\]player_[0-9]+[\\](?<name>[A-Za-z0-9]*)\b";
        Regex re = new Regex(rex);


            for (Match m = re.Match(stringData); m.Success; m = m.NextMatch())
        {

            players[count] = (m.Groups["name"]).ToString();
            textBox1.Text += players[count] + "\r\n";
            MessageBox.Show((m.Groups["name"]).ToString());
        }

The above pattern kinda works, but only if the playername is using A-Za-z0-9. I didnt realise at the time that playername can contain any character. can someone modify this pattern for me please, regex is beyond me :<

Thanks in advance.


Provided the rest of the regex works as desired, you could replace

(?<name>[A-Za-z0-9]*)

with

(?<name>[^\\]*)

which will match everything except \

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号