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"
["#X", "+", "23", ">=", "#Threshold", "&&", "#X", "*", "4", "!=", "54", "||", "IgnoreCheck"]
&& || != + = - * / ^ > < <= >=
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