Regular Expression Help
Alex Martelli
aleaxit at yahoo.com
Fri Oct 27 06:20:32 EDT 2000
<kentsin at poboxes.com> wrote in message
news:mailman.972621076.17776.python-list at python.org...
> Is it possible for regular expression to match optional parameters
> like :
>
> func(optional parameters)
>
> in which the func name and some optional parameters be match for
> later retrieval. To match the func is easy, but if the optional
> parameters can be
>
> ()
> (p)
> (p1, p2)
> ....
>
> is it possible to match these?
It depends on what form of 'optional parameters' you need to match.
If each optional parameter can be a general expression, containing
maybe parentheses, commas, etc, then this is no job for RE's IMHO --
a little parser seems more adequate. If you know a parameter
cannot include parentheses, then:
r'(\w+)\(([^)]*)\)'
will match, and return as group 1, the whole content of the
parentheses (a string split of that on ',' will then separate
the optional parameters out as a list, if they can't contain
commas either) -- if you need to check that, e.g., there are
never two adjacent commas, you can then do it with a pass on
the parameters list (check that none is empty, or whitespace
only, etc, etc).
Alex
More information about the Python-list
mailing list