Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
string[] num = Regex.Split(expr, @"\(|\)|\-|\+|\*|\/").Where(s => !String.IsNullOrEmpty(s)).ToArray();
为此,我得到了操作符和大括号。
使用lookaround,即lookahead和lookbehind来分割输入
(?<=\(|\)|\-|\+|\*|\/)|(?=\(|\)|\-|\+|\*|\/) ^
如果没有环顾四周,正则表达式引擎会拆分这些字符并吃掉它,即它不会在结果中显示它
如果你想评估数学表达式,看看这些
.NET 中是否有字符串数学评估器?
从数学表达式创建二叉树