Regular Expression in C#

Regular expressions in C# are represented by the Regex class in the System.Text.RegularExpressions namespace. The Regex class provides methods for matching, replacing, and manipulating text using regular expressions. Here’s an example of how you can use regular expressions in C#:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Hello, my phone number is (555) 123-4567.";
        string pattern = @"\(\d{3}\) \d{3}-\d{4}";

        // Create a Regex object with the pattern
        Regex regex = new Regex(pattern);

        // Use the Match method to find the first occurrence of the pattern in the input
        Match match = regex.Match(input);

        // Check if a match was found
        if (match.Success)
        {
            Console.WriteLine("Phone number found: " + match.Value);
        }
        else
        {
            Console.WriteLine("Phone number not found.");
        }
    }
}

In this example, the regular expression pattern @"\(\d{3}\) \d{3}-\d{4}" is used to match a phone number in the format “(555) 123-4567”. The \d represents a digit, and {3} and {4} specify that there should be three and four consecutive digits, respectively.

The Match method is called on the Regex object to search for the pattern in the input string. If a match is found (match.Success is true), the matched phone number is printed to the console. Otherwise, a message indicating that no phone number was found is displayed.

This is a simple example, and regular expressions can be much more complex and powerful. The Regex class provides additional methods for replacing text, splitting strings, and more. You can refer to the official documentation for more information on the Regex class and its methods: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex

Regex Class:

The Regex class in C# is part of the System.Text.RegularExpressions namespace and provides functionality for working with regular expressions. It represents a compiled representation of a regular expression pattern and provides methods for matching, replacing, and manipulating text based on that pattern.

Here are some commonly used methods of the Regex class:

  1. Match: Searches the input string for the first occurrence of the regular expression pattern and returns a Match object representing the match.
  2. Matches: Searches the input string for all occurrences of the regular expression pattern and returns a collection of Match objects.
  3. MatchCollection: Represents a collection of Match objects returned by the Matches method.
  4. Replace: Replaces all occurrences of the regular expression pattern in the input string with a specified replacement string and returns the modified string.
  5. Split: Splits an input string into an array of substrings based on the occurrences of the regular expression pattern.
  6. IsMatch: Indicates whether the regular expression pattern matches any part of the input string.
  7. Group: Represents a captured group within a regular expression match.
  8. Groups: Represents a collection of captured groups within a regular expression match.

Here’s an example that demonstrates the usage of some of these methods:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "The quick brown fox jumps over the lazy dog.";
        string pattern = @"\b\w{5}\b"; // Matches words with exactly 5 letters

        // Create a Regex object with the pattern
        Regex regex = new Regex(pattern);

        // Use the Matches method to find all occurrences of the pattern in the input
        MatchCollection matches = regex.Matches(input);

        // Iterate over the matches and print the matched words
        foreach (Match match in matches)
        {
            Console.WriteLine("Match found: " + match.Value);
        }

        // Use the Replace method to replace all occurrences of the pattern with "dog"
        string replaced = regex.Replace(input, "dog");
        Console.WriteLine("Replaced string: " + replaced);

        // Use the Split method to split the input string into an array of words
        string[] words = regex.Split(input);
        Console.WriteLine("Split words:");
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }

        // Use the IsMatch method to check if the input contains any 5-letter words
        bool isMatch = regex.IsMatch(input);
        Console.WriteLine("Is there a match? " + isMatch);
    }
}

In this example, the regular expression pattern \b\w{5}\b is used to match words in the input string that have exactly 5 letters. The \b represents a word boundary, and \w{5} specifies that there should be exactly 5 consecutive word characters.

The Matches method is called on the Regex object to find all occurrences of the pattern in the input string. The resulting MatchCollection is then iterated over to print the matched words.

The Replace method is used to replace all occurrences of the pattern with the word “dog” in the input string. The Split method is used to split the input string into an array of words based on the pattern. The IsMatch method is used to check if the input string contains any 5-letter words.

These are just a few examples of the methods provided by the Regex class. It offers various other methods and properties for more advanced regular expression operations. You can refer to the official documentation for more details and examples: https://docs.microsoft.com/en-us/dotnet

Regex Replace String Example in C# :

Certainly! Here’s an example of using the Regex.Replace method in C# to replace a specific pattern within a string:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Hello, my name is John Doe.";
        string pattern = @"\bJohn\b"; // Matches the word "John" as a whole word

        // Create a Regex object with the pattern
        Regex regex = new Regex(pattern);

        // Use the Replace method to replace the matched pattern with a new string
        string replaced = regex.Replace(input, "Jane");

        Console.WriteLine("Original string: " + input);
        Console.WriteLine("Replaced string: " + replaced);
    }
}

In this example, the input string is “Hello, my name is John Doe.” The regular expression pattern \bJohn\b matches the word “John” as a whole word, where \b represents a word boundary.

The Replace method is called on the Regex object to replace the matched pattern with the string “Jane”. The resulting replaced string is stored in the replaced variable.

When you run this code, the output will be:

Original string: Hello, my name is John Doe.
Replaced string: Hello, my name is Jane Doe.

The pattern \bJohn\b is found in the input string and replaced with “Jane”.

You can use the Regex.Replace method to perform various types of replacements based on regular expression patterns in a string. The replacement can be a fixed string or you can use match groups to create more dynamic replacements.

Find the Duplicate Words in Regex C# :

To find duplicate words in a string using regular expressions in C#, you can use the Regex.Matches method and a capturing group. Here’s an example:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "The quick brown fox jumps over the lazy dog dog.";

        // Define the regular expression pattern to match duplicate words
        string pattern = @"\b(\w+)\b(?=.*\b\1\b)";

        // Create a Regex object with the pattern
        Regex regex = new Regex(pattern);

        // Use the Matches method to find all occurrences of duplicate words
        MatchCollection matches = regex.Matches(input);

        // Iterate over the matches and print the duplicate words
        Console.WriteLine("Duplicate words found:");
        foreach (Match match in matches)
        {
            Console.WriteLine(match.Value);
        }
    }
}

In this example, the regular expression pattern \b(\w+)\b(?=.*\b\1\b) is used to match duplicate words in the input string. Let’s break down the pattern:

  • \b represents a word boundary.
  • (\w+) is a capturing group that matches one or more word characters.
  • (?=.*\b\1\b) is a positive lookahead assertion that ensures the captured word is followed by the same word boundary and the same captured word, indicating a duplicate occurrence.

The Matches method is called on the Regex object to find all occurrences of the duplicate words in the input string. The resulting MatchCollection is then iterated over to print the duplicate words found.

When you run this code, the output will be:

Duplicate words found:
dog

In this example, the word “dog” appears twice in the input string, so it is considered a duplicate word.

Note that this example finds duplicate words based on exact word matches. If you want to consider case-insensitive matches or handle variations such as punctuation or whitespace differences, you can modify the regular expression pattern accordingly.