How to Split Text Using Regex
In this article, you will see how you can split text strings in C# using regular expressions (regex). Though, the C# programming language contains Split() function from the String class that you can use to split strings in C#, with Regex you have more control over splitting patterns.
To split a string using the Regex in C#, you need to call the Split() method using the Regex class from the “System.Text.RegularExpressions“ module. The Split() method returns a list of all the split substrings.
Let’s see some examples of how you can split strings using Regex in C#.
Note: You will need to import the “System.Text.RegularExpressions” module before running the script below.
- Splitting a String using Spaces
- Splitting a String Using Alphabets
- Splitting a String using Digits
- Get Digits By Splitting a String
- Splitting a String Using Special Characters
Splitting a String using Spaces
To split a string using spaces as the delimiters, you need to use the regex expression “\s+”. The input string and the regex expression are passed to the Split() method of the Regex class as shown in the script below, where the input string contains two spaces.
class Program { static void Main(string[] args) { string input = "1253A8 4556B4 113C"; string regex = @"\s+"; var substrings = Regex.Split(input, regex, RegexOptions.IgnoreCase); foreach(string val in substrings) { Console.WriteLine(val); } Console.ReadLine(); } }
Output
From the above output, you can see that the input string is split into three substrings at the points where spaces occur.
Splitting a String Using Alphabets
To split a string using alphabets, you can use the “[a-z]” as the regex expression for the Split() method. This regex expression looks for all the alphabets from a to z. To make sure that the matching is case insensitive, you can pass “RegexOptions.IgnoreCase” as the third parameter value to the split method.
In the script below, the input string contains three alphabets at different locations, in the output of the script below, you will see three substrings created by splitting input strings using alphabets as delimiters.
You can see that the character “C” is at the last index in the string. The split() method will return an empty string for “C” which you will not see in the output.
class Program { static void Main(string[] args) { string input = "1253A84556B4113C"; string regex = @"[a-z]"; var substrings = Regex.Split(input, regex, RegexOptions.IgnoreCase); foreach(string val in substrings) { Console.WriteLine(val); } Console.ReadLine(); } }
Output
Splitting a String using Digits
To split a string using digits, you can use the regex expression “\d” inside the Split() method. For instance, in the script below, the input string contains three digits i.e. 5, 6, and 9 at different locations within the string.
In the output of the script below, you will see four substrings returned by the Split() method.
class Program { static void Main(string[] args) { string input = "Manual5Automatic6Great9Awesome"; string regex = @"\d"; var substrings = Regex.Split(input, regex, RegexOptions.IgnoreCase); foreach(string val in substrings) { Console.WriteLine(val); } Console.ReadLine(); } }
Output
Get Digits By Splitting a String
You can also use the regex Split() method to get all the digits from your input string. To do so, you need to pass “\D+” as the regex expression to the Split() method.
Let’s see this with the help of an example. The input string in the script below contains three digits: 5, 6, and 9. When you run the script below, you will see that these digits are printed in the output.
class Program { static void Main(string[] args) { string input = "Manual5Automatic6Great9ass"; string regex = @"\D+"; var substrings = Regex.Split(input, regex, RegexOptions.IgnoreCase); foreach(string val in substrings) { if (!string.IsNullOrEmpty(val)) { int i = int.Parse(val); Console.WriteLine(i); } } Console.ReadLine(); } }
Output
Splitting a String Using Special Characters
Finally, you can also split your strings using special characters. To do so, you can use the regex expression [^\w].
As an example, the input string in the script below contains three special characters: #, ?, and $. In the output of the script below, you will see four substrings created using these special characters as the delimiters.
class Program { static void Main(string[] args) { string input = "Manual#Automatic?Great$Awesome"; string regex = @"[^\w]"; var substrings = Regex.Split(input, regex, RegexOptions.IgnoreCase); foreach(string val in substrings) { Console.WriteLine(val); } Console.ReadLine(); } }
Output:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C
- How to Split Text Using Regex