Girrafish
Girrafish

Reputation: 2482

Split mathematical expression on every operator/bracket

I'm trying to split math expressions using regex although no matter what I try, there's always one condition that I'm missing.
I need to split the string on all operators +,-,*,/ and on all types of brackets (,),[,]. If there's an increment or decrement ++,-- I need to keep it as one string. I also have to keep multidigit numbers as one string.

Right now, the closest thing I have is:
(?<![+-[0-9]])(?=[*/+-\[\]\(\)])|(?<=[*/+-\[\]\(\)])(?![+-[0-9]])

For example, [(([++[[(--2)]*33]])/22)+1] should give
[,(,(,[,++,[,[,(,--,2,),],*,33,],],),/,22,),+,1,] but instead gives
[,(,(,[,++,[,[,(,--2,),],*,33,],],),/,22,),+1,]

Upvotes: 2

Views: 223

Answers (1)

j6m8
j6m8

Reputation: 2399

tldr: Other developers that inherit your code will be able to understand the \d+ regex... But they're very unlikely to be able to look at your monster of a regex — or any other long regex, for that matter — and understand what's going on.

If there's a simpler way to do it without regexes, that's usually the better option for both readability and performance. But if you must use regex...

My recommendation is to use the much simpler \d+ to find all digits, or \D to find all non-digits; then, you should loop through the result and combine any subsets of the array tokens where tokens[i] and tokens[i+1] are both + or -.

As commenters have noted, this is still likely less performant than ignoring RegExes altogether and splitting using a loop.

While regular expressions are interesting and very useful for certain tasks, matching-bracket tasks like this is particularly poorly suited for regex (because the math syntax we commonly use is not a regular language).

Upvotes: 2

Related Questions