I'm trying to parse some functions arguments with PyParsing but am having trouble getting the right syntax. Namely, given:
str = "(key=val())"
I would like the parser to return ['key', 'val()']
.
I've been trying to get this to work with the following code; the .suppress()
calls are intentionally omitted for clarity.
ob = Literal("(")
cb = Literal(")")
key = Word(alphas)
value = Word(alpha + "()")
parser = ob + key + "=" + value + cb
print parser.parseString(str)
but of course it's matching the final closing bracket as well and so I get a ParseException.
Is there an elegant solution to this? For example, I looked at nestedExpr
but in this case it's not strictly a nesting since I want the val()
to be treated as a literal. Similarly this question alludes to the problem but doesn't give a solution.