Documente Academic
Documente Profesional
Documente Cultură
http://www.dotnetperls.com/regex-split
Go
1 of 5
30-10-2011 18:57
http://www.dotnetperls.com/regex-split
Notes. The input string contains the numbers 10, 20, 40 and 1, and the static Regex.Split method is called with two parameters. The string @"\D+" is a verbatim string literal that designates all NON-digit characters. When a regex pattern has an escaped uppercase letter like \D, it means NOT. Get Numbers From String
2 of 5
30-10-2011 18:57
http://www.dotnetperls.com/regex-split
string[] operands = Regex.Split(operation, @"\s+"); // // Now we have each token. // foreach (string operand in operands) { Console.WriteLine(operand); } } } Output 3 * 5 = 15
Notes on tokenizers. Computer programs and languages first undergo lexical analysis and tokenization, which gets all the 'tokens' such as those shown in the output above. This is an effective way to parse computer languages or program output.
3 of 5
30-10-2011 18:57
http://www.dotnetperls.com/regex-split
// // Get all uppercased words. // var list = new List<string>(); foreach (string value in uppercaseWords) { // // Check the word. // if (!string.IsNullOrEmpty(value) && char.IsUpper(value[0])) { list.Add(value); } } // // Write all proper nouns. // foreach (var value in list) { Console.WriteLine(value); } } } Output Bob Michelle Indiana
More examples
First, for performance you may want to try using the string Split method, which is an instance method on the string type, instead of regular expressions. That method is more appropriate for precise and predictable input. Split String Examples Compiled and instance Regex. You can also change the Regex.Split method call into an instance Regex, which enhances performance and reduces memory pressure. Additionally, you can use the RegexOptions.Compiled enumerated constant for greater performance. Regex Performance RegexOptions.Compiled
4 of 5
30-10-2011 18:57
http://www.dotnetperls.com/regex-split
Summary
We looked at how you can extract strings with the Regex.Split method, using patterns of non-digit characters, whitespace characters, and non-word characters. We also saw how you can process the string array result of Regex.Split, such as by parsing the integers in a sentence. I suggest that using loops on the results of Regex.Split is an easy way to further filter your results. Regex Overview
5 of 5
30-10-2011 18:57