Type of regular expression

Joakim Hove hove at bccs.no
Fri Jul 2 04:15:33 EDT 2004


Peter Otten <__peter__ at web.de> writes:

> Joakim Hove wrote:
>
>> I wondered how I could test wether an argument was of type compiled
>> regexp:
>
> Both strings and compiled regular expressions can be compiled:

[...]

> Therefore I would not test beforehand, just rely on re.compile() to know
> what it can deal with:
>
>>>> def do_something(s):
> ...     try:
> ...             s = re.compile(s)
> ...     except TypeError:
> ...             sys.exit("Something went wrong")
> ...     # more stuff
> ...

Thanks for answering, however I am afraid i posed the question
somewhat ambigously: The point is that i want the function to do
different things depending on the type of input:

def do_something(arg):
    if type(arg) is RegexpType:
       # Handle regular expression argument
    elsif type(arg) is StringType:
       # Handle string argument - which is something
       # completely different.
    else:
       sys.exit("Wrong argument type")


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