Regular Expression In C#

In C#, Regular Expression serves to parse and validate provided text against a specified pattern, such as an email address. This pattern may encompass various operators, character literals, or constructs.

To handle text using regular expressions in the .NET Framework, typically, the regular expression engine is employed. In C#, the Regular Expression is represented by the Regex class.

Regular Expression

We employed Regular Expressions to validate if the provided string conforms to a specific pattern. Regular Expression, commonly known as Regex, is a string of characters that outlines the said pattern. This pattern may consist of digits, constants, operators, symbols, and more. Patterns are utilized for searching through strings or documents to determine if matches are present. This process helps us identify whether the specified criteria are met.

Regular Expressions are typically employed for tasks such as parsing, locating strings, or performing validations.

Here we are considering a scenario where Regular Expressions can be applied for validating social security numbers, verifying dates of birth, matching full names with first and last names divided by a comma, substituting substrings, ensuring proper email formats, formatting currency, and more.

Regex Class

The Regex class represents the regular expression engine within the .NET Framework. It serves the purpose of scanning extensive text content to identify particular character patterns. This class offers functionality for operations such as extraction, modification, substitution, or removal of specific substrings within the text.

Within the System.Text.RegularExpressions namespace, you can find the Regex class, which requires a string pattern as a parameter along with additional optional parameters.

Now, we are going to generate a regular expression pattern. Within this script, our objective is to identify words that commence with the character 'M' by applying the pattern matching process.

Example

// Create a pattern for the word that starts with letter "M"  
string pattern = @"\b[M]\w+";  
// Create a Regex  
Regex rgex = new Regex(pattern);

Here is a code snippet that includes a lengthy text with the author's name that needs to be extracted.

Example

// Long string  
string authors = "Ramesh chand, Rakeshwar";

Here we will utilize the Matches method to locate all occurrences, resulting in the MatchCollection.

Example

// Get all matches  
MatchCollection matchedAuthors = rg.Matches(authors);
To find the matches collection we will use the For loop // Print all matched authors  
for (int count = 0; count < matchedAuthors.Count; count++)  
Console.WriteLine(matchedAuthors[count]. Value);

Now, let's consider an example to locate the character 'M'.

Example

// Create a pattern for a word which starts with letter "M"  
string pattern1 = @"\b[M]\w+";  
// Create a Regex  
Regex rg = new Regex(pattern1);  
// Long string  
string Authors = "Ramesh Chand,Rakeshwar"; 
// Get all matches  
MatchCollection matchedAuthors = rg.Matches(Authors );  
// For print all matched authors  
for (int count = 0; count < matchedAuthors.Count; count++)  
Console.WriteLine(matchedAuthors[count].Value);

When searching for the character 'M' in the given example, a scenario may occur where the word starts with a lowercase 'm'. To address this situation, we can utilize the RegexOption.IgnoreCase parameter. This parameter enables the Regex to disregard the distinction between uppercase and lowercase letters, ensuring a case-insensitive search.

Example

// Create a pattern for a word that starts with letter "M"  
string pattern1 = @"\b[m]\w+";  
// Create a Regex  
Regex rgex = new Regex(pattern, RegexOptions.IgnoreCase);

Regular Expression Example in C#

Here we are using an example to verify the format of an email address. In this case, we will utilize the Regex class.

Example

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string email = "support@example.com";

            var result = Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");

            Console.Write("Is valid: {0} ", result);

            Console.ReadLine();
        }
    }
}

In the provided example, we are verifying whether the input string conforms to a valid format. To accomplish this task, we are employing the Regex class. The validation of the input text involves utilizing the IsMatch method in conjunction with a Regular Expression pattern.

After running the aforementioned code, the output is generated as depicted below:

The <style> element is styled with a linear gradient background, a border radius of 12px, padding of 40px, and a margin of 20px on the top and bottom to provide spacing. The content is centered within the element. Within this element, there is an icon with a font size of 3rem and a margin at the bottom of 10px. The text within this element is colored #9ca3af and has a font size of 1rem. </style>

Regex Class method in C#

To execute different actions on the input string, the Regex class includes a variety of functions. The following table presents a compilation of different Regex methods in C#.

Method Description
IsMatch We used the IsMatch method for finding out whether the given input string matches the regular expression pattern or not.
Matches Matches method is used to return the text, which matches with the regular expression pattern.
Replace Replace method is used to replace the text, which matches the regular expression pattern.
Split We used the Split method for splitting the string into the array of the substring at those positions, which matches with the regular expression pattern.

The above method of Regex class is used for the validation, replacement, or splitting the values of the string with the regular expression pattern, which is based on the requirements.

Regex Replace String Example in C#

By utilizing this illustration, we aim to identify the substring through a regular expression pattern in C# that substitutes the specified values.

Example

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Hi,welcome@Java.com";

            string result = Regex.Replace(str, "[^a-zA-Z0-9_]+", " ");

            Console.Write("{0} ", result);

            Console.ReadLine();
        }
    }
}

In the previous example, we employed the Regex Replace method to identify and substitute special characters in a string with spaces. This was achieved by utilizing a regular expression pattern ("[^a-zA-Z0-9_]+").

In the previous example, the Regular Expression pattern ("[^a-zA-Z0-9_]+") attempted to identify a single character that does not fall within the specified character group.

After running the program mentioned above, the resulting output will be displayed as follows:

The code snippet below illustrates the styling for a placeholder element:

Example

.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }

Find the Duplicate Words in Regex C#

By employing the regular expression pattern, we can readily identify duplicate words.

In this illustration, we aim to identify the repeated word within the provided string by employing the Regex class function in C#.

Example

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program1
    {
        static void Main(string[] args)
        {
            string str1 = "Welcome To to Java. Here we can learn C# easily easily way";

            MatchCollection collection = Regex.Matches(str1, @"\b(\w+?)\s\1\b", RegexOptions.IgnoreCase);

            foreach (Match m in collection)

            {

                Console.WriteLine("{0} (duplicates '{1}') at position {2}", m.Value, m.Groups[1].Value, m.Index);

            }

            Console.ReadLine();
        }
    }
}

The previous instance employed Regex. The Matches function is utilized to identify duplicate words through the regular expression pattern ("\b(\w+?)\s\1\b").

After running the code above, the output will be as displayed below:

Input Required

This code uses input(). Please provide values below: