Search code examples
regexevalironpythonwhitelist

IronPython Regular Expression | ^$ issue


I have 2 regular expressions, 1 to pull out nested functions (which are then evaluated in reverse order), and another to confirm they are whitelisted to go through eval (Once I hopefully get this working, I want XML save and configuration data to be parsed and evaluated).

Here's the expression to grab the functions and their parameters. It was working until I added the |^$ bit, but I want that bit so I can support/whitelist functions that take no parameters.

func = re.compile(r"""([^\(]*) #first group stores function name 
  \( #starting parenthesis
  \b([^\)\(\]\[\}\{]+)|^$\b #second group saves parameters passed to function
  \) #closing parenthesis
  """, re.VERBOSE)

Here's the whitelist expression. It also worked until I added ^$ to support functions without parameters.

whitelist = re.compile(r"""
  \bint|float|print|addthese|aval|^$\b #list of allowable procedures (or no procedure)
  \( #starting parenthesis
  \b[^\)\(\]\[\}\{]+|^$\b #parameters passed to function
  \) #closing parenthesis
  """,re.VERBOSE)

And I'm (attempting to) parse this, which is intentionally nonsense right now but just to prove out the concept. The intention is to evaluate the whitelisted expressions and be able to deal with parenthesis used for math.

print(float(addthese(int(((1)+(2)+aval())),1)))

Solution

  • You need an optional non-capturing group:

    ([^\(]*) #first group stores function name 
    \( #starting parenthesis
     (?:  # optional non-capturing group start
       \b([^\)\(\]\[\}\{]+)\b  #second group saves parameters passed to function
      )?  # optional non-capturing group end
    \) #closing parenthesis
    

    See the regex demo.

    The (?:...)? makes the whole \b([^\)\(\]\[\}\{]+)\b pattern sequence optional.

    Note you overescaped the pattern, you may use

    ([^(]*) #first group stores function name 
    \( #starting parenthesis
     (?:  # optional non-capturing group start
       \b([^][(){}]+)\b  #second group saves parameters passed to function
      )?  # optional non-capturing group end
    \) #closing parenthesis