Link Search Menu Expand Document

Regular Expressions in C#

Regular expressions are pattern matching favored for string parsing and replacement. They are away for a pc character to express how a laptop software should exhibit up for a different sample in textual content fabric and what the software is to do when each pattern in structure is found.

The Regex Class

The Regex classification is used for representing an everyday expression. It has the following usually used methods:

public bool IsMatch(string input)

Reflects whether or not the day-to-day expression high-quality in the Regex constructor finds a shape in a particular enter string.

public bool IsMatch(string input, int start)

Reveals whether or not the starter expression sure in the Regex constructor discovers a structure in the exact enter string, opening at the unique opening characteristic in fixed.

public static bool series(string input, string pattern)

Reflects whether or not or not the unique everyday expression discovers a healthy in the unique enter string.

public MatchCollection Matches(string input)

Searches the positive enter string for all happenings and an everyday expression.

public string Replace(string input, string replacement)

A terrific input string replaces all traces that wholesome a standard expression sample with a unique substitute string.

public string[] Split(string input)

Divides an enter string into a formation of substrings at the roles described with the aid of a regular expression pattern distinct in the Regex constructor.

Special Characters

^

Word after this thing matches at the opening of the string or line.

$

Word earlier than this element suits at the quit of the line or string

.\d

It is used to healthy the digit character

.\D

It is used to shape the non-digit character

.\w

It is used to healthy any alphanumeric and underscores the character

.\W

It is used to healthy any non-word character

.\s

It is used to shape the white-space characters

.\S

It is used to shape the non-white-space characters

\n

It is used to suit a newline.

Example 1

The following example matches words that start with ‘S’.

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);

         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      static void Main(string[] args) {
         string str = "A Thousand Splendid Suns";
         Console.WriteLine("Matching words that start with 'S': ");
         showMatch(str, @"\bS\S*");
         Console.ReadKey();
      }
   }
}

 

When the above code is assembled and implemented, it generates the following result.

Matching words that start with ‘S’: The Expression: \bS\S* Splendid Suns

 

Example 2

The following example matches words that start with ‘m’ and end with ‘I.

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);

 

         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }

      static void Main(string[] args) {
         string str = "make maze and manage to measure it";

         Console.WriteLine("Matching words start with 'm' and ends with 'e':");
         showMatch(str, @"\bm\S*e\b");
         Console.ReadKey();
      }
   }
}

When the above code is assembled and implemented, it generates the following result.

Matching words start with 'm' and ends with 'I:
The Expression: \bm\S*e\b
make
maze
manage
measure

Example 3

This example replaces extra white space.

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      static void Main(string[] args) {
         string input = "Hello   World   ";
         string pattern = "\\s+";
         string replacement = " ";

         Regex rgx = new Regex(pattern);
         string result = rgx.Replace(input, replacement);

         Console.WriteLine("Original String: {0}", input);
         Console.WriteLine("Replacement String: {0}", result);   
         Console.ReadKey();
      }
   }
}

When the above code is assembled and implemented, it generates the following result.

Original String: Hello World  
Replacement String: Hello World

Conclusion

We discovered what regular expression is along with the syntax/symbols that are used to denote, construct a regular expression. Regular expression lets the person fit a string with a given pattern. a regular expression is a pretty compelling device for a programmer and helps in decreasing the quantity of code that is required to accomplish facts matching or a validation task.

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy