Link Search Menu Expand Document

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.

  1. Splitting a String using Spaces
  2. Splitting a String Using Alphabets
  3. Splitting a String using Digits
  4. Get Digits By Splitting a String
  5. 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

Regular Expressions Split Text

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

Regular Expressions Text Splitting

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

Reg Ex Split Text

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

Regular Expressions Split a String

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:

Regular Expressions Split by Special Character

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy