0

I'm trying to match a string pattern like this recursively or iteratively.

term :- can be any variable(string) or integer
expression :- term + expression or term * expression or term / expression or term - expression

The idea is to find a matching pattern for mathematical expression like this

ZING^30*sin(45)+46*LENGTH-20

Let's assume that the term is ready.

As usual, I'll start from a very bad one... in java.

[[term\\+]*[term\\*]*[term\\\]*[term\\-]*]+term
4
  • 1
    Looks like you might have a grammar that can be used with a recursive descent parser there... No clue if you can use regex for that though Commented Jun 3, 2014 at 5:03
  • Can you please elaborate on Input and expected Output with an example. Commented Jun 3, 2014 at 5:10
  • Your example contains a sin() function, which doesn't match the description you have given of your expression grammar. Commented Jun 3, 2014 at 10:58
  • @EJP I think it matches the "variable(string)" definition of "term", assuming "variable" means any identifier and "string" means any sequence of characters (possibly excluding parentheses). At least that's what I assumed. The requirements aren't clear. Commented Jun 3, 2014 at 15:37

2 Answers 2

1

You're using the wrong tool for the job. Regular expressions can't describe grammars with recursive syntaxes, by definition. Look up the Chomsky Hierarchy. You need an expression parser. Look up the Dijkstra Shunting-yard algorithm, or 'recursive descent expression parser'.

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

Comments

-1

This looks like homework, so I'm not going to give you a complete answer. If you know how to use moderately complex regexes, you should be able to come up with a regex to represent your "term"--it shouldn't be too difficult. Now the regex for "expression" will be a term, followed by zero or more occurrences of "operator followed by term" (operator is one of +, -, *, /, maybe ^?). That is, expression will be "term", or "term op term", or "term op term op term", etc.

Note: this is how you'd write a regex that tells you whether the input has the correct format, but it won't help you break it down into parts. For that, you'd need a different regex (or two) that you would match in a loop. I can't tell just what your requirements are, though.

Comments

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.