Andoni Zubizarreta Andoni Zubizarreta - 4 years ago 100
C# Question

Regex expression to divide mathematical and logical expression C#

I'm trying to find a way of dividing an expression into parts with regex. I'm currently splitting everything by spaces but that's not the correct way of doing it. Here is an example expression:

var expression = "#X+ 23>=#Threshold && #X * 4 != 54 || #IgnoreCheck"


The result I'm looking for is a list of all the parts:

["#X", "+", "23", ">=", "#Threshold", "&&", "#X", "*", "4", "!=", "54", "||", "IgnoreCheck"]


The characters to take into account for spliting:

&& || != + = - * / ^ > < <= >=

Answer Source

You may use

var res = Regex.Split(expression, @"\s*(&&|\|\||<=|>=|!=|[-+=*/^><])\s*");

See the regex demo. The main idea is to use alternation (|) since some alternatives are multicharacter strings.

Details

  • \s* - 0+ whitespace
  • (&&|\|\||<=|>=|!=|[-+=*/^><]) - capturing group 1 (thus, when used with Regex.Split, these parts will be output into the resulting array) matching
    • && - two ampersands
    • | - or
    • \|\| - 2 pipes
    • | - or
    • <= - a < and then =
    • | - or
    • >= - a > and then =
    • | - or
    • != - ! followed with =
    • | - or
    • [-+=*/^><] - any 1 symbol from this set
  • \s* - 0+ whitespace

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download