Valhalla Legends Archive

Programming => Battle.net Bot Development => Topic started by: MyndFyre on July 15, 2004, 08:00 PM

Title: .NET Wildcarding
Post by: MyndFyre on July 15, 2004, 08:00 PM
While the Visual Basic language has a nifty little Like operator, such a convenience is lacking in the other .NET languages.  Most of us are familiar with the * and ? wildcards that we had access to in DOS, and like I said - it's easy to get with VB, but not so much with other languages.

Here's what I came up with to simulate it efficiently with the .NET FCL (this is generally the code in my command parser, for multi-ban, which is a helper function called if a wildcard is detected in the argument):


// top of file
using System.Text.RegularExpressions;

// in the class
      #region helper function         banmulti
      private void banmulti(
         UserBase   commander,
         bool      whispered,
         string[]   args
         )
      {
         // In
         string pattern = args[0].Replace(
            "?", ".").Replace(
            "*", ".*");

         Regex re = new Regex(pattern, RegexOptions.IgnoreCase);

         int len = myConnection.UsersInChannel.Length;

         for (int i = 0; i < len; i++)
         {
            if (re.IsMatch(myConnection.UsersInChannel[i]))
            {
               // check flags
               // issue ban if flags don't prevent ban
            }
         }
      }
      #endregion


myConnection is my connection manager instance, which maintains a list of the users currently in the channel (in an array property called UsersInChannel, suprisingly enough).

Hope that helps someone out.  :)
Title: Re:.NET Wildcarding
Post by: Maddox on July 16, 2004, 02:10 AM
How about using regular expressions to match users? That would be even more neat.


...nevermind.
Title: Re:.NET Wildcarding
Post by: MyndFyre on July 16, 2004, 04:54 AM
Quote from: Maddox on July 16, 2004, 02:10 AM
How about using regular expressions to match users? That would be even more neat.

I'm not quite sure what you mean..............  That _is_ what the regular expressions do.....
Title: Re:.NET Wildcarding
Post by: Maddox on July 16, 2004, 06:33 AM
Weird, I didn't see that in your code the first time around.  :-\
Title: Re:.NET Wildcarding
Post by: K on July 16, 2004, 03:52 PM
Depending on how many times you're going to be calling .IsMatch(), you might want to consider using the RegExOptions.Compiled flag.
Title: Re:.NET Wildcarding
Post by: c0ol on July 19, 2004, 10:35 AM
Quote from: Maddox on July 16, 2004, 02:10 AM
How about using regular expressions to match users?

If you mean like

.ban ^llama(.*?)\d{3}


I found users to resent this type of matching for obvious reasons =\
Title: Re:.NET Wildcarding
Post by: Adron on July 19, 2004, 10:44 AM
Quote from: c0ol on July 19, 2004, 10:35 AM

.ban ^llama(.*?)\d{3}


I found users to resent this type of matching for obvious reasons =\

Well, they couldn't match that particular user without regular expressions. If you're only using the same features available with wildcards, regular expressions aren't that complicated.
Title: Re:.NET Wildcarding
Post by: MyndFyre on July 19, 2004, 03:29 PM
Quote from: c0ol on July 19, 2004, 10:35 AM
Quote from: Maddox on July 16, 2004, 02:10 AM
How about using regular expressions to match users?

If you mean like

.ban ^llama(.*?)\d{3}


I found users to resent this type of matching for obvious reasons =\

The point of my problem was, I'm not particularly familiar with Regular Expressions -- as to how they function in .NET.  I knew that was probably the way I wanted to go just to get the old DOS ? and * wildcards, but I didn't know *how* to do that.

So, the input matching string is like this:

*[vL]

then my statement converts it into appropriate regular expression syntax:

.*[vL]

which matches correctly.
Title: Re:.NET Wildcarding
Post by: Adron on July 19, 2004, 05:13 PM
Quote from: Myndfyre on July 19, 2004, 03:29 PM
So, the input matching string is like this:

*[vL]

then my statement converts it into appropriate regular expression syntax:

.*[vL]

which matches correctly.

Shouldn't that be .*\[vL]$ or possibly .*\[vL\]$ ?
Title: Re:.NET Wildcarding
Post by: MyndFyre on July 19, 2004, 06:01 PM
Quote from: Adron on July 19, 2004, 05:13 PM
Quote from: Myndfyre on July 19, 2004, 03:29 PM
So, the input matching string is like this:

*[vL]

then my statement converts it into appropriate regular expression syntax:

.*[vL]

which matches correctly.

Shouldn't that be .*\[vL]$ or possibly .*\[vL\]$ ?

You may be correct; however, I've tested it out to work the way it is.  Actually, yes, if I recall, [ and ] are control characters (I haven't done extensive testing).  It might be a good idea for me to look for control characters and escape them out.  Thanks for the heads-up. :)
Title: Re:.NET Wildcarding
Post by: c0ol on July 20, 2004, 12:40 AM
this is what I did in perl once upon a time...

sub convertglob {
   my $thing = shift;
   $thing = quotemeta($thing);
   $thing = "^$thing\$";
   $thing =~ s/\\\*/\.\*/g;
   $thing =~ s/\\\?/\./g;
   my $re;
   {
      no re 'eval';
      $re = eval { qr/$thing/i };
   }
   return $re;
}

take it for what it's worth (not much prolly)
Title: Re:.NET Wildcarding
Post by: Adron on July 20, 2004, 10:30 AM
Quote from: Myndfyre on July 19, 2004, 06:01 PM
You may be correct; however, I've tested it out to work the way it is.  Actually, yes, if I recall, [ and ] are control characters (I haven't done extensive testing).  It might be a good idea for me to look for control characters and escape them out.  Thanks for the heads-up. :)

Could you try matching *[vL] (using your wildcarder) against a name such as "[vL]Adron" and see if it matches that? I was thinking it might match any substring.