Adding wildcard_to_regexp() to functions_general and cleaning up some imports.

This commit is contained in:
Greg Taylor 2007-05-23 17:51:31 +00:00
parent 8335f8b80f
commit 7d78cbcf4b
3 changed files with 24 additions and 5 deletions

View file

@ -7,6 +7,30 @@ import commands_unloggedin
General commonly used functions.
"""
def wildcard_to_regexp(instring):
"""
Converts a player-supplied string that may have wildcards in it to regular
expressions. This is useful for name matching.
instring: (string) A string that may potentially contain wildcards (* or ?).
"""
regexp_string = ""
# If the string starts with an asterisk, we can't impose the beginning of
# string (^) limiter.
if instring[0] != "*":
regexp_string += "^"
# Replace any occurances of * or ? with the appropriate groups.
regexp_string += instring.replace("*","(.*)").replace("?", "(.{1})")
# If there's an asterisk at the end of the string, we can't impose the
# end of string ($) limiter.
if instring[-1] != "*":
regexp_string += "$"
return regexp_string
def cmd_check_num_args(session, arg_list, min_args, errortext="Missing arguments!"):
"""
Check a player command's splitted argument list to make sure it contains