Author | Message | Time |
---|---|---|
Myndfyr | 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): [code] // 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 [/code] 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. :) | July 16, 2004, 1:00 AM |
Maddox | How about using regular expressions to match users? That would be even more neat. ...nevermind. | July 16, 2004, 7:10 AM |
Myndfyr | [quote author=Maddox link=board=17;threadid=7724;start=0#msg70739 date=1089961833] How about using regular expressions to match users? That would be even more neat. [/quote] I'm not quite sure what you mean.............. That _is_ what the regular expressions do..... | July 16, 2004, 9:54 AM |
Maddox | Weird, I didn't see that in your code the first time around. :-\ | July 16, 2004, 11:33 AM |
K | Depending on how many times you're going to be calling .IsMatch(), you might want to consider using the RegExOptions.Compiled flag. | July 16, 2004, 8:52 PM |
c0ol | [quote author=Maddox link=board=17;threadid=7724;start=0#msg70739 date=1089961833] How about using regular expressions to match users? [/quote] If you mean like [code] .ban ^llama(.*?)\d{3} [/code] I found users to resent this type of matching for obvious reasons =\ | July 19, 2004, 3:35 PM |
Adron | [quote author=c0ol link=board=17;threadid=7724;start=0#msg71308 date=1090251336] [code] .ban ^llama(.*?)\d{3} [/code] I found users to resent this type of matching for obvious reasons =\ [/quote] 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. | July 19, 2004, 3:44 PM |
Myndfyr | [quote author=c0ol link=board=17;threadid=7724;start=0#msg71308 date=1090251336] [quote author=Maddox link=board=17;threadid=7724;start=0#msg70739 date=1089961833] How about using regular expressions to match users? [/quote] If you mean like [code] .ban ^llama(.*?)\d{3} [/code] I found users to resent this type of matching for obvious reasons =\ [/quote] 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. | July 19, 2004, 8:29 PM |
Adron | [quote author=Myndfyre link=board=17;threadid=7724;start=0#msg71357 date=1090268976] So, the input matching string is like this: *[vL] then my statement converts it into appropriate regular expression syntax: .*[vL] which matches correctly. [/quote] Shouldn't that be .*\[vL]$ or possibly .*\[vL\]$ ? | July 19, 2004, 10:13 PM |
Myndfyr | [quote author=Adron link=board=17;threadid=7724;start=0#msg71377 date=1090275184] [quote author=Myndfyre link=board=17;threadid=7724;start=0#msg71357 date=1090268976] So, the input matching string is like this: *[vL] then my statement converts it into appropriate regular expression syntax: .*[vL] which matches correctly. [/quote] Shouldn't that be .*\[vL]$ or possibly .*\[vL\]$ ? [/quote] 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. :) | July 19, 2004, 11:01 PM |
c0ol | this is what I did in perl once upon a time... [code] 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; } [/code] take it for what it's worth (not much prolly) | July 20, 2004, 5:40 AM |
Adron | [quote author=Myndfyre link=board=17;threadid=7724;start=0#msg71388 date=1090278115] 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. :) [/quote] 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. | July 20, 2004, 3:30 PM |