python - Can You Use a Single Regular Expression to Parse Function Parameters? -
problem
there program file contains following code snippet @ point in file.
... food($apples$ , $oranges$ , $pears$ , $tomato$){ ... } ...
this function may contain number of parameters must strings separated commas. parameter strings lowercase words.
i want able parse out each of parameters using regular expression. example resulting list in python follows:
["apples", "oranges", "pears", "tomato"]
attempted solution
using python re module, able achieve breaking problem 2 parts.
find function in code , extract list of parameters.
plist = re.search(r'food\((.*)\)', programstring).group(1)
split list using regular expression.
params = re.findall(r'[a-z]+', plist)
question
is there anyway achieve 1 regular expression instead of two?
edit
thanks tim pietzcker's answer able find related questions:
to answer question "can done in single regex?": yes, not in python.
if want match , capture (individually) unknown number of matches in example, using single regular expression, need a regex engine supports captures (as opposed capturing groups). .net , perl 6 currently.
so in python, either need in 2 steps (find
entire food(...)
function call, , findall
individual matches second regex suggested dingo).
or use parser paul mcguire's pyparsing
.
Comments
Post a Comment