• Welcome to Valhalla Legends Archive.
 

.NET Wildcarding

Started by MyndFyre, July 15, 2004, 08:00 PM

Previous topic - Next topic

MyndFyre

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

Maddox

#1
How about using regular expressions to match users? That would be even more neat.


...nevermind.
asdf.

MyndFyre

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

Maddox

Weird, I didn't see that in your code the first time around.  :-\
asdf.

K

Depending on how many times you're going to be calling .IsMatch(), you might want to consider using the RegExOptions.Compiled flag.

c0ol

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 =\

Adron

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.

MyndFyre

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

Adron

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\]$ ?

MyndFyre

#9
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. :)
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.

c0ol

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)

Adron

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.