Regular expression query

  • Thread starter Martin Biddiscombe
  • Start date
M

Martin Biddiscombe

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

bruno at modulix

Martin said:
It's probably quite simple, but what I want is a regular expression

If it's simple, then you probably *dont* want a regexp.
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?

yes : forget regexps, use str methods.

parse = lambda l: \ l.split('=',1)[1].split()[0].strip().strip("'\"")

NB : I tried my best to make it as obfuscated as a regexp so you still
gain extra bonus points from Perl-addicts !-p - but feel free to rewrite
this cleanly.


HTH
 
T

Tim Chase

"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)
 
G

Giovanni Bajo

Martin said:
"parameter=12ab"
"parameter=12ab foo bar"
"parameter='12ab'"
"parameter='12ab' biz boz"
"parameter="12ab""
"parameter="12ab" junk"
.... s = s.split("=")[1]
.... s = shlex.split(s)[0]
.... return s
....'12ab'
 
B

bruno at modulix

Giovanni said:
Martin Biddiscombe wrote:

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

... s = s.split("=")[1]
... s = shlex.split(s)[0]
... return s

I definitevely have to learn and use the shlex module.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top