Sunteți pe pagina 1din 5

C# Regex.

Split Method Examples

http://www.dotnetperls.com/regex-split

Go

C# Regex.Split Method Examples


You want to extract groups of characters in a string based on complex rules, and the string Split method in the C# programming language is not adequate. For example, you must get all numbers, tokens, or uppercased words in a string. Here we look at the Regex.Split method, which is ideal for this purpose.

Dot Net Perls

String numbers example


First, here we look at how you can get all numbers in a string, and then actually parse them into integers for easier usage in your C# program. The important part of the example is that it splits on all non-digit values in the string, and then loops through the result strings and uses int.TryParse.
Program that uses Regex.Split [C#] using System; using System.Text.RegularExpressions; class Program { static void Main() { // // String containing numbers. // string sentence = "10 cats, 20 dogs, 40 fish and 1 programmer."; // // Get all digit sequence as strings. // string[] digits = Regex.Split(sentence, @"\D+"); // // Now we have each number string. // foreach (string value in digits) { // // Parse the value to get the number.

1 of 5

30-10-2011 18:57

C# Regex.Split Method Examples

http://www.dotnetperls.com/regex-split

// int number; if (int.TryParse(value, out number)) { Console.WriteLine(value); } } } } Output 10 20 40 1

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

Multiple whitespaces example


Here we see how you can extract all substrings in your string that are separated by whitespace characters. You could also use string Split, but this version is simpler and can also be extended more easily. The example gets all operands and operators from an equation string.
Program that tokenizes [C#] using System; using System.Text.RegularExpressions; class Program { static void Main() { // // The equation. // string operation = "3 * 5 = 15"; // // Split it on whitespace sequences. //

2 of 5

30-10-2011 18:57

C# Regex.Split Method Examples

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.

Uppercase words example


Here we look at a method that gets all the words that have an initial uppercase letter in a string. The Regex.Split call used actually just gets all the words, and the loop checks the first letter for its case. In most programs, it is useful to combine regular expressions and manual looping and string operations. Programs are not art projects.
Program that collects uppercase words [C#] using System; using System.Collections.Generic; using System.Text.RegularExpressions; class Program { static void Main() { // // String containing uppercased words. // string sentence = "Bob and Michelle are from Indiana."; // // Get all words. // string[] uppercaseWords = Regex.Split(sentence, @"\W");

3 of 5

30-10-2011 18:57

C# Regex.Split Method Examples

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

C# Regex.Split Method Examples

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

S-ar putea să vă placă și