简体   繁体   中英

Regex.Split command in c#

I am trying to use Regex.SPlit to split aa string in order to keep all of its contents, including the delimiters i use. The string is a math problem. For example, 5+9/2*1-1. I have it working if the string contains a + sign but I don't know how to add more then one to the delimiter list. I have looked online at multiple pages but everything I try gives me errors. Here is the code for the Regex.Split line I have: (It works for the plus, Now i need it to also do -,*, and /.

string[] everything = Regex.Split(inputBox.Text, @"(\+)");

Use a character class to match any of the math operations: [*/+-]

string input = "5+9/2*1-1";
string pattern = @"([*/+-])";
string[] result = Regex.Split(input, pattern);

Be aware that character classes allow ranges, such as [0-9] , which matches any digit from 0 up to 9. Therefore, to avoid accidental ranges, you can escape the - or place it at either the beginning or end of the character class.

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