1

I have a bunch of strings representing mathematical functions (which could be nested and have any number of arguments), and I want to be able to use regex to return an array of strings, each string being an argument of the outer-most function. Here's an example:

"f1(f2(x),f3(f4(f5(x,y,z))),f(f(1)))"

I would want a regex pattern that I could use to somehow get an array of all the arguments of f1, which in this case are the strings "f2(x)", "f3(f4(f5(x,y,z)))", and "f(f(1))". There will be no spaces in the input string.

Thank you very much to anyone who can help.

1 Answer 1

4

I don't think this can be done with regexes alone.

This would probably require being able to identify balanced parentheses -- for example, once we've parsed f1(f2(x), the next character could either be a ) or a , -- and that's a canonical example of something that can't be done with regexes, but requires a more sophisticated parser.

Sign up to request clarification or add additional context in comments.

10 Comments

Dang, really? This is gonna be rough then. Know of any Java utilities that already do anything close to this?
The reason this is generally impossible with regexp is that regexp have no memory. There is no general way to let it match nested items, and that's the case with the f3(...) construct above.
The most general way to do this sort of thing is with a tool like ANTLR. Alternatively, roll your own stack-based solution, or a recursive descent parser...but it's not doable with regexes.
The bright side is that a recursive descent parser will do this easily and are easy to write.
You could, for example, use a regex to match on just the first and last parentheses in a string, and then recurse on the things between the parentheses.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.