如果希望分隔符为“自己的分隔符”,则可以使用Regex.Split例如:
string input = "plum-pear";string pattern = "(-)";string[] substrings = Regex.Split(input, pattern); // Split on hyphensforeach (string match in substrings){ Console.WriteLine("'{0}'", match);}// The method writes the following to the console:// 'plum'// '-'// 'pear'因此,如果您要拆分数学公式,则可以使用以下正则表达式
@"([*()^/]|(?<!E)[+-])"
这将确保您还可以使用常量,例如1E-02,并避免将它们拆分为1E,-和02
所以:
Regex.Split("10E-02*x+sin(x)^2", @"([*()^/]|(?<!E)[+-])")产量:
10E-02
*
x
+
sin
(
x
)
^
2



