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.

  1. find function in code , extract list of parameters.

    plist = re.search(r'food\((.*)\)', programstring).group(1) 
  2. 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:

  1. python regular expressions - how capture multiple groups wildcard expression?
  2. which regex flavors support captures (as opposed capturing groups)?

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

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -