Type of regular expression

Joakim Hove hove at bccs.no
Thu Jul 1 10:54:40 EDT 2004




Hello,

I wondered how I could test wether an argument was of type compiled
regexp:

from types import *

def do_something(arg):
   if type(arg) is StringType:
      print "%s is a string" % arg
   elsif type(arg) is RegexpType:       # <- This constant does not
                                        #    exist, it is only
                                        #    something I invented.
      print "%s is a regexp" % arg
   else
      sys.exit("Argument must be *either* string or regular expression")

which is then called as:

do_something("a string")
do_something(re.compile("^F")





Regards

Joakim

-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove at bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/



More information about the Python-list mailing list