I'm trying to parse some function arguments using PyParsing, but it's hard for me to get the correct syntax. Namely, given:
str = "(key=val())"
I would like the parser to return ['key', 'val()'].
I tried to get this to work with the following code; challenges are .suppress()intentionally dropped 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 also matches the final closing bracket, and therefore I get a ParseException.
Is there an elegant solution? For example, I looked at nestedExpr, but in this case it is not strictly a nest, since I want to val()be treated as a literal. Similarly, this question refers to a problem, but does not provide a solution.