Regular expression query
Tim Chase
python.list at tim.thechases.com
Fri Feb 3 10:56:22 EST 2006
> "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( *|$)')
Below is a test-harness that seemed to spit out the results you
want (I threw in some bogus tests to make sure they failed too)
with the given value for "exp".
The resulting match object will have your desired value in
group(1)...though it will include whatever quotes happened to be
in it. You may also need to anchor accordingly with "^" and "$"
It doesn't gracefully handle escaped quotes in your value
-tim
import re
tests = [
('parameter=12ab', True),
('parameter=12ab foo bar', True),
("parameter='12ab'", True),
("parameter='12ab' biz boz", True),
('parameter="12ab"', True),
('parameter="12ab" junk', True),
('parameter="12ab', False),
('parameter=\'12ab', False),
('parameter="12ab\'', False),
('parameter="12ab\' foo baz', False)
]
exp = r'parameter=((["\'])(.*?)\2|[^\'" ]+).*'
r = re.compile(exp)
print "Using regexp: %s" % exp
for test,expectedResult in tests:
if r.match(test):
result = True
else:
result = False
if result == expectedResult:
print "[%s] passed" % test
else:
print "[%s] failed (expected %s, got %s)" % (test,
expectedResult, result)
More information about the Python-list
mailing list