Regex.Split. This C# method separates strings based on a pattern. It handles a delimiter specified as a pattern—such as "\D+" which means non-digit characters.
Using a Regex yields a greater level of flexibility and power than string.Split. The syntax is more complicated, and performance may be worse.
Whitespace example. Here we extract all substrings that are separated by whitespace characters. We could use string.Split. But this version is simpler and can be more easily extended.
Info The example gets all operands and operators from an equation string. An operand is a character like "*" that acts on operands.
Part 1 With Regex, we implement a simple tokenizer. Lexical analysis and tokenization is done in many programs.
Part 2 We display results. This may be an effective way to parse computer languages or program output, but it is not the fastest way.
using System;
using System.Text.RegularExpressions;
// Input string.
string operation = "3 * 5 = 15";
// Part 1: split it on whitespace sequences.
string[] operands = Regex.Split(operation, @"\s+");
// Part 2: display results.// ... Now we have each token.
foreach (string operand in operands)
{
Console.WriteLine(operand);
}3
*
5
=
15
Number example. We use Regex.Split to split on all non-digit values in the input string. We then loop through the result strings, with a foreach-loop, and use int.TryParse.
using System;
using System.Text.RegularExpressions;
// 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.
int number;
if (int.TryParse(value, out number))
{
Console.WriteLine(value);
}
}10
20
40
1
Uppercase. Here we get all the words that have an initial uppercase letter in a string. The Regex.Split call gets all the words. And the foreach-loop checks the first letters.
Tip It is often useful to combine regular expressions and manual looping and string operations.
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
// String containing uppercased words.
string sentence = "Bob and Michelle are from Indiana.";
// Get all words.
string[] uppercaseWords = Regex.Split(sentence, @"\W");
// 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);
}Bob
Michelle
Indiana
For performance consider the string Split method (on the string type) instead of regular expressions. That method is more appropriate for precise and predictable input.
Also You can change the Regex.Split method call into an instance Regex. This enhances performance and reduces memory pressure.
Further You can use the RegexOptions.Compiled enumerated constant for greater performance.
Summary. We extracted strings with the Regex.Split method. We used patterns of non-digit characters, whitespace characters, and non-word characters.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 18, 2024 (simplify).