Regular expression query

Martin Biddiscombe public at priatel.co.uk
Fri Feb 3 09:20:43 EST 2006


It's probably quite simple, but what I want is a regular expression to
parse strings of the form:

"parameter=12ab"
"parameter=12ab foo bar"
"parameter='12ab'"
"parameter='12ab' biz boz"
"parameter="12ab""
"parameter="12ab" junk"

in each case returning 12ab as a match. "parameter" is known and fixed.
The parameter value may or may not be enclosed in single or double
quotes, and may or may not be the last thing on the line. If the value
is quoted, it may contain spaces.

I've tried a regex of the form:
re.compile(r'parameter=(["\']?(.*?)\1( *|$)')

This works fine when the parameter's value is quoted, but if the quotes
are missing, it falls over since the \1 is empty and so the non-greedy
"match anything" ends up matching nothing.

Any suggestions?

Thanks

<M>




More information about the Python-list mailing list