简体   繁体   中英

How can I get numbers, operators and braces from regular expressions in C#?

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

为此,我得到了运算符括号

Use lookaround ie lookahead and lookbehind to split the input

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM