In a particular example, I want to extract function calls in a string input (ruby script), to do some statistics (in java). For an example input of:
Math.sqrt(2-Math.hypot((3),4))-factorial(5)
I want to get a list of possible functions used (verification does not need to be 100% accurate, and it can include some extra faulty guesses) :
{ Math.sqrt, Math.hypot, factorial }
List does not have to be case sensitive, but it should include function class path if it exists.
I tried naively simplistic ".*\\((.*)\\)", but I could not get it to work. It seems, that I need to use lookaheads or backreference, but I’m a bit stumped. My question is, can I even do this?
No. You
cannotshould not (see edit below) do this.Regular expressions can only match regular languages, but bracket
()matching is required to match function calls since there can be nested expressions like((1+ 2)*3)in a function call. Regular expressions cannot deal with nested parentheses.To learn more about regular languages and the limits of regular expressions, see Regular Expressions (wikipedia)
To solve your particular problem, you might be interested in the following resources, which recommend importing the ruby script and using reflection (wikipedia)
Edit: If all you want is the function name it is possible that you might get regex to work. However: