1
string[] num = Regex.Split(expr, @"\(|\)|\-|\+|\*|\/").Where(s => !String.IsNullOrEmpty(s)).ToArray();

For this I am getting operators and braces.

dda
  • 6,030
  • 2
  • 25
  • 34
  • `\d` http://www.regular-expressions.info/reference.html – Patashu Jun 27 '13 at 06:23
  • i tried \d it works if i use in new string array. – user2499557 Jun 27 '13 at 06:28
  • What do you want to do? Do you want to take something like (4 + 4) and turn it into "(" "4" "+" "4" ")" in a string array? – Patashu Jun 27 '13 at 06:30
  • ya something like tat. I am calculating an expression but stuck in providing precedence for expression if i take all numbers,operators and braces in single array then i can calculate eassily. – user2499557 Jun 27 '13 at 06:39

1 Answers1

1

Use lookaround i.e lookahead and lookbehind to split the input

 (?<=\(|\)|\-|\+|\*|\/)|(?=\(|\)|\-|\+|\*|\/)
                       ^

Without lookaround the regex engine would split on those characters and eat it i.e it won't show it in the result

If you want to evaluate mathematical expressions,have a look at these

Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • thank you but i dont want binary tree because i am trying something new. Cant i evaluate expression by using regular expression. – user2499557 Jun 27 '13 at 06:47
  • @user2499557 **NO**..you can't evaluate expression with regex only..i tried that once and failed to evaluate properly..after splitting the input,change it into postfix notation and evaluate using stack..check out the first link – Anirudha Jun 27 '13 at 06:50