In C#, we use the Regex
class to represent a regular expression. It performs various operations like pattern matching, replacing patterns, and others on strings. It is defined in the System.Text.RegularExpressions
namespace.
The syntax for the Regex
class is as follows:
Regex regexObj = new Regex(pattern);
Where,
regexObj
: This is an example of the Regex
class. pattern
: This is a regular expression pattern. Regex(String)
: This initializes a new instance of the regular expression class that contains the regular expression specified in pattern parameter. Regex(String,RegexOptions)
: It initializes a new instance of the regular expression class that contains the regular expression pattern specified in the pattern parameter and also specifies an option that modifies the regular expression. Regex(String,RegexOptions,TimeSpan)
: It initializes a new instance of the regular expression class that contains the regular expression pattern specified in the pattern parameter, specifies an option that modifies the regular expression, and also specifies a time-out interval.CacheSize
: We use the function to get or set the size of the internal cache array used for storing the compiled regular expression pattern and corresponding regex objects. CultureInvariant
: It returns a value that indicates whether the regular expression specified in the Regex
constructor is defined as case-insensitive for all cultures or not.ECMAScript
: It returns a value that indicates whether the regular expression specified in the Regex
constructor is defined as case-insensitive for all cultures or not.IgnoreCase
: We use the function to get or set a value that indicates whether the Regex
object is defined as case-insensitive.IsMatchTimeout
: We use this function to get a value that indicates whether the time-out interval has elapsed.MatchTimeout
: We use this function to get or set a time-out interval in milliseconds. If no time-out value is defined, the method uses the default time-out interval.Multiline
: It is used to get or set a value that indicates whether the Regex
object searches an input string for all matches or only for the first match.RightToLeft
: It is used to get or set a value that indicates whether the search for a regular expression pattern is from right to left or from left to right. Matches(String)
: It searches an input string for all occurrences of a regular expression and returns information about each occurrence as a collection of match objects. Matches(String,Int32)
: It searches the specified input string for all occurrences of a regular expression, beginning at the specified starting position in the string, and returns information about each occurrence as a collection of match objects. The example for using Regex
class code in C# is as follows:
using System;using System.Text.RegularExpressions;class Program{static void Main(string[] args){// using the Regex classRegex regex = new Regex("abc");// checking if a given string matches a regular expressionConsole.WriteLine(regex.IsMatch("abc")); // trueConsole.WriteLine(regex.IsMatch("Abc")); // false// getting the first matchMatch match = regex.Match("abcdefabc");Console.WriteLine(match); // abc// getting all the matchesMatchCollection matches = regex.Matches("abcdefabc");foreach (Match m in matches)Console.WriteLine(m); // abc// abc}}
In the above code:
True
, as it matches. False
, as it does not match.