1

Examples:

input: (n!/(1+n))
output: frac{n!}{1+n}

input: ((n+11)!/(n-k)^(-1))
output: frac{(n+11)!}{(n-k)^(-1)}

input: (9/10)
output: frac{9}{10}

input: ((n+11)!/(n-k)^(-1))+(11)/(2)
output: frac{(n+11)!}{(n-k)^(-1)}+(11)/(2)

The following regex works if there are no sub parentheses.

\(([^\/\)]*)\/([^\)]*)\)

The following does matching parentheses

@\((([^()]++|\((?:[^()]++|(?R))+\))+)\)@

I just can not figure out how to "combine" them - write a single regex to handle division and balanced parentheses.

Kobi
  • 135,331
  • 41
  • 252
  • 292
SamB
  • 2,621
  • 4
  • 34
  • 39
  • 3
    Regular expressions are not a good tool for complex parsing - *especially* if it needs to match balanced text. You can sort of hack something up with the extensions that some regex variants provide, but you really want to be using an actual parser for this sort of thing. – Anon. Jan 17 '11 at 20:09
  • 1
    The thing is, I am so close. I don't really know anything about a parser and it would take more time to implement than I would like. I know it is possible with regex and for me that is the easiest way. – SamB Jan 17 '11 at 20:16
  • I've added an answer, which *at least* is working for the test cases. Feel free to modify it - it's far from perfect (you can add possessive quantifiers, and maybe a unary `+` if you're brave). The biggest issue with it is that is treats all `/` as a fraction, with no orders of operators: `a+2/3+b` -> `(a+2)/(3+b)`. – Kobi Jan 17 '11 at 21:24

2 Answers2

1

I think something like this should work:

((?:\w+|\((?1)\))(?:[+*^-](?1)|!)?)\/((?1))

Now, this probably isn't perfect, but here's the idea:

The first group, $1, is ((?:\w+|\((?1)\))(?:[+*^-](?1)|!)?), which is:

(A literal) or (a balanced expression wrapped in parentheses), followed by an optional operator and another balanced expression if needed.
Writing it that way, we can use (?1) anywhere in the regex to refer to another balanced expression.

Working example: http://ideone.com/PNLOD

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • Thanks for the work so far. I only need this to work when there is a division sign inside parentheses. This is close enough though, I will be able to figure it out from here. Thanks! – SamB Jan 17 '11 at 21:20
  • @SamB - no problem! It's an interesting question, by the way, and the input/output example are very clear. You're right, I missed the outer parentheses, but I think it's workable. – Kobi Jan 17 '11 at 21:30
  • @Kobi Is there any chance you could help me out adapting this regex to a javascript regex compatible format? – adolfosrs Dec 12 '20 at 17:04
  • @adolfosrs - [JavaScript doesn't support recursive regex](https://stackoverflow.com/q/4414339/7586), so it cannot be adapted. Easiest option here is to ask a new question, I'm sure someone knows a better tool for JavaScript. – Kobi Dec 13 '20 at 13:23
0

I know you've already accepted a regexp, but the real answer to what you're trying to do is a proper parser... and no, you don't need to code this from scratch or reinvent the wheel. While a lot of people hate phpclasses, the evalMath class that you can find there is incredibly useful for parsing and evaluating mathematical formulae. See my answer to a similar question for details of how it can be used and extended.

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Mark: I am actually not doing any math evaluation. I am parsing math and automatically converting it into LaTeX. Is there a parser that would work well for that? – SamB Jan 18 '11 at 19:59
  • @SamB - Try the answers here: http://stackoverflow.com/questions/2421768/php-based-latex-parser-where-to-begin – Spudley Apr 16 '12 at 20:36