2

I am trying to parse a line of code for a mathematical operation.

Such as: 4+18/(9-3)

I want the sections to be: 4, +, 18, /, (, 9, -, 3, and ). So basically, all numbers, operators, and parenthesis.

If I use split with delimiters it removes the delimiter upon finding it. So if I use my operators as delimiters, well then they're removed.

I tried using Regex.split and it INCLUDES the operator, but not how I want it. It produces results like 4+, since it includes the 4 and the plus.

Here's what I've been trying:

        string convert = "4+8/(9-3)";

        string[] parts = Regex.Split(convert, @"(?<=[-+*/])");
5
  • You can use Regex to parse a string, but you cannot use regex to parse syntax. You would be better off using a math expression parser/evaluator. Commented Mar 31, 2018 at 23:46
  • But, to answer your question anyway, ([\d\.]+|[()e^/%x*+-]) should work. (example) Commented Mar 31, 2018 at 23:51
  • If you plan to work with integers only, Regex.Split(convert, @"(\D)");. But it will not work as an end solution. Commented Mar 31, 2018 at 23:57
  • i am not sure but do u want numbers and signs separate ? Commented Apr 1, 2018 at 0:06
  • See stackoverflow.com/questions/4392022/… Commented Apr 1, 2018 at 0:47

2 Answers 2

4

As per my comment if you intend to use this to evaluate a math expression you should not use Regex as you cannot have any syntactic or semantic understanding of a string you're parsing.

If you're not intending to do that, this should work fine

([\d\.]+|[()e^/%x*+-])

Regexr example

Dotnetfiddle example

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

3 Comments

@Aran-Fey You're correct, changed from Regex.Split to Regex.Matches
This added some extra "" empty string entries for me but I just created a quick loop to remove them. Not sure if I did something wrong to include them, but once the empty strings were removed which was easy enough the results so far from my testing have been desirable. Thanks.
@Birdman It's because of how Regex.Split works, if you check the Dotnetfiddle example I use Regex.Matches and loop through the matches it returns instead.
-1

Well, I guess the problem could be solved with the help of the following regex.

[-+*/\(\)]|\d+

7 Comments

Nope. This splits numbers into single digits.
Nope. Now it duplicates every character. And even if you change that capture group to a non-capturing group it still would split numbers into single digits. Please test your regex; this is getting silly.
"it still would split numbers into single digits" - how is that, @Aran?
Well, because it doesn't matter if you use \d or \d+ in a lookbehind. After the regex passes the first digit, the lookbehind sees a digit and the number is split into two, + or not.
@Aran I didnt think of lookbehind but now im actually not sure why op used it all
|

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.