• Welcome to Valhalla Legends Archive.
 

C#.net Regex

Started by Michael, January 02, 2009, 09:48 PM

Previous topic - Next topic

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.)




        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;
        }


just incase i have included the kick command also.


                    #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


Spht

Quote from: -MichaeL- on January 02, 2009, 09:48 PM

        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;
        }


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

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.

Michael

#3
I decided to use Plan B:


 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;
}



usage

if (wildcardcheck("*x*", "12x34")
{
MessageBox.Show("Cookies");
}

MyndFyre

QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Warrior

A well written homebrew wildcard implementation is probably faster than using Regular Expressions.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

MyndFyre

Quote from: Warrior on January 04, 2009, 11:42 PM
A well written homebrew wildcard implementation is probably faster than using Regular Expressions.
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.  :)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.