Valhalla Legends Forums Archive | .NET Platform | C#.net Regex

AuthorMessageTime
-MichaeL-
For my battle.net ops bot

[21:37:49] <ZhaoYun-Shu-@USEast> ?kick zhao*
[21:37:49] ZhangFei-Shu- was kicked out of the channel by Messiah.

It shouldn't have kicked ZhangFei, i've looked through everything i could find on regex and created this prepare check code does anything seem wrong? (I use this same code for tagbans and also have a problem where it bans someone it shouldn't every once and a while.)



[code]
        public string preparecheck(string tocheck)
        {
            if (tocheck.StartsWith("*"))
            {
                tocheck = tocheck.Remove(0, 1);
            }
            tocheck = tocheck.Replace("]", "¢");
            tocheck = tocheck.Replace("[", "£");
            tocheck = tocheck.Replace("+", "¤");
            tocheck = tocheck.Replace(".", "¥");
            tocheck = tocheck.Replace("$", "¦");
            tocheck = tocheck.Replace("?", "§");
            tocheck = tocheck.Replace("#", "¬É");
            tocheck = tocheck.Replace("@", "©");
            return tocheck;
        }
[/code]

just incase i have included the kick command also.

[code]
                    #region kick
                    if (command == "kick")
                    {
                        int a = getuser(username);
                        if (a >= getcommand(command.ToLower()))
                        {
                            if (bnettext[1].Contains('*'))
                            {
                                Regex toban = new Regex(tagban.preparecheck(bnettext[1].ToLower()));
                                string tocheck = "";
                                for (int i = 0; i != Users.Count; i++)
                                {
                                    string tosend1 = "";
                                    tocheck = tagban.preparecheck(Users[i].ToString().ToLower());
                                    Match ismatch = toban.Match(tocheck);
                                    if (ismatch.Success == true)
                                    {
                                        if (bnettext.Length > 3)
                                        {
                                            for (int gg = 2; i < bnettext.Length; gg++)
                                            {
                                                tosend1 = tosend1 + " " + bnettext[gg];
                                            }
                                        }
                                        else if (bnettext.Length == 3)
                                        {
                                            tosend1 = bnettext[2];
                                        }
                                        if (getuser(formatname(Users[i].ToString().ToLower())) == -1)
                                        {
                                            qtadd("/kick " + Users[i].ToString() + " " + tosend1);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                int b = getuser(formatname(bnettext[1]));
                                if (a > b)
                                {
                                    string tosend;
                                    tosend = bnettext[1];
                                    for (int i = 2; i < bnettext.Length; i++)
                                    {
                                        tosend = tosend + " " + bnettext[i];
                                    }
                                    qtadd("/kick " + tosend);
                                }
                                else
                                {
                                    qtadd("Cannot kick a user with equal or greater access");
                                }
                            }
                        }
                    }
                    #endregion
[/code]
January 3, 2009, 3:48 AM
Spht
[quote author=-MichaeL- link=topic=17768.msg181009#msg181009 date=1230954488]
[code]
        public string preparecheck(string tocheck)
        {
            if (tocheck.StartsWith("*"))
            {
                tocheck = tocheck.Remove(0, 1);
            }
            tocheck = tocheck.Replace("]", "¢");
            tocheck = tocheck.Replace("[", "£");
            tocheck = tocheck.Replace("+", "¤");
            tocheck = tocheck.Replace(".", "¥");
            tocheck = tocheck.Replace("$", "¦");
            tocheck = tocheck.Replace("?", "§");
            tocheck = tocheck.Replace("#", "¬É");
            tocheck = tocheck.Replace("@", "©");
            return tocheck;
        }
[/code]
[/quote]

0x1 to 0x1f are reserved and can't be transmitted over b.net chat.  why not use those as wildcard restrictors?  also, you should allow the '?' wildcard.  it never appears in usernames and it can be very useful
January 3, 2009, 2:59 PM
K
I would suspect that you are constructing your regular expression wrong.

the regular expression "zhao*" means match the string 'zha' followed by 0 or more of the character 'o', which does indeed match "Zhang".  If you want to treat the "*" as a set of any characters, you need to prefix it with a period.
January 4, 2009, 5:24 PM
-MichaeL-
I decided to use Plan B:

[code]
 public bool wildcardcheck(string A, string B)
{
A = A.ToLower();
B = formatforcheck(B);
if (A.StartsWith("*") && A.EndsWith("*"))
{
A = A.Replace("*", null);
if (B.Contains(A))
{
return true;
}
}
else if (A.StartsWith("*"))
{
A = A.Replace("*", null);
if (B.EndsWith(A))
{
return true;
}
}
else if (A.EndsWith("*"))
{
A = A.Replace("*", null);
if (B.StartsWith(A))
{
return true;
}
}
else
{
return false;
}
return false;
}

[/code]

usage
[code]
if (wildcardcheck("*x*", "12x34")
{
MessageBox.Show("Cookies");
}
[/code]
January 4, 2009, 6:56 PM
Myndfyr
[quote author=-MichaeL- link=topic=17768.msg181019#msg181019 date=1231095399]
I decided to use Plan B:
[/quote]

A.k.a. "Plan Suckage"? :P
January 4, 2009, 9:40 PM
Quarantine
A well written homebrew wildcard implementation is probably faster than using Regular Expressions.
January 5, 2009, 5:42 AM
Myndfyr
[quote author=Warrior link=topic=17768.msg181033#msg181033 date=1231134141]
A well written homebrew wildcard implementation is probably faster than using Regular Expressions.
[/quote]
Mmm, I don't know.  You can safely convert simple wildcard implementations into regex and then cache and compile them (auto-generate assemblies on the fly) - since that's built-in to regex in .net, it's free.  Unless "well-written" incorporates that as well.  :)
January 5, 2009, 9:06 AM

Search