Msdn regex LEARNOVITA

Regular Expression in C# Tutorial | Everything You Need to Know

Last updated on 16th Aug 2022, Blog, Tutorials

About author

Jagan Mohan (Senior C# Developer )

Jagan Mohan is a Senior Senior C# Developer and has extensive knowledge in the following areas of WPF, XML, REST, Agile, V model, C#, SQL server, Web API, and ASP.NET.

(5.0) | 18639 Ratings 2267

Msdn regex

Regex

The term Regex means for Regular expression. The regex or regexp or regular expression is a sequence of various characters which describe the particular search pattern. It is also called a Rational expression.It is majorly used for searching and manipulating text strings.Can simply search the pattern and replace them with the matching pattern with the help of regular expression.

Regular Expression Characters

There are different type of characters of a regular expression:

  • 1. Metacharacters
  • 2. Quantifier
  • 3. Groups and Ranges
  • 4. Escape Characters or character classes

C# Regex class syntax

C# Regex

The basic syntax are used for regular expressions are,

Quantifiers

The very important quantifiers are *?+.

1. * => Match the preceding character zero or more times.

2. + => Match the preceding character 1 or more times.

3. ? => Match the preceding char zero or one time.

Special characters:

More special characters are available for regex building. Here are some of the more usual ones.

1. ^ => It is used to match the starting of a string.

2. $=> It is used to match the end of the string.

3. (Dot) => Matches any character to only once.

4. \d => It is used to match the digit character.

5. \D => It is used to match any non-digit character.

6. \w => It is used to match the alphanumeric character plus “_”.

7. \W => It is used to match any non-word character.

8. \s => Matches the white space characters.

9. \S => Matches the non-white space character.

10. \n =>Matches the newline character.

Constructors in Regex Class

Constructors in Regex

The various constructors and their description is given:

Table: Constructors in Regex Class in C#

Source: MSDN

    ConstructorsDescription
    Regex() This constructor initiates a new instance of the Regex class.
    Regex(SerializationInfo, StreamingContext) This constructor initiates a new instance of the Regex class by using serialized data.
    Regex(String) This constructor initiate a new instance of the Regex class for the particular regular expression
    Regex(String, RegexOptions) This constructor initiates a new instance of the Regex class for the particular regular expression, with options that modify the pattern
    Regex(String, RegexOptions, TimeSpan) This constructor initiates a new instance of the Regex class for the particular regular expression, with options that modify the pattern and a value that specifies how long a pattern matching method should attempt the match before it times out.

Fields in Regex Classz

The various fields and their description is given as follows:

Table: Fields in Regex Class in C#

    FieldsDescription
    Capnames This field is used by a Regex object created by the CompileToAssembly method.
    Caps This field is used by a Regex object created by the CompileToAssembly method.
    Capsize This field is used by a Regex object created by the CompileToAssembly method.
    Capslist This field is used by a Regex object created by the CompileToAssembly method.
    Factory This field is used by a Regex object created by the CompileToAssembly method.
    InfiniteMatchTimeout This timeout is used by a Regex object created by the CompileToAssembly method.
    internalMatchTimeout This timeout is the maximum amount of time that can elapse in the pattern-matching operation before operation timed out.
    pattern This field is used by a Regex object created by the CompileToAssembly method.
    roptions This field is used by a Regex object created by the CompileToAssembly method.

Properties in Regex Class

The various properties and their description is given as follows:

Table: Properties in Regex Class in C#

Source: MSDN

    PropertiesDescription
    CacheSize This property gets or sets the maximum number of entries in a current static cache of compiled regular expressions.
    CapNames This property gets or sets a dictionary that maps named the capturing groups to their index values.
    Caps This property gets a dictionary that maps numbered capturing groups to the index values.
    MatchTimeout This property gets the time-out interval of a current instance.
    Options This property gets the options that were passed into a Regex constructor.
    RightToLeft This property gets a value that denotes whether the regular expression searches from right to left.

Methods in Regex Class

The various methods and their description is given:

Table: Methods in Regex Class in C#

Source: MSDN

    .
    MethodsDescription
    CompileToAssembly(RegexCompilationInfo[], AssemblyName) This method compiles one or more particular Regex objects to a named assembly.
    Equals(Object) This method determines whether the particular object is equal to the current object.
    Escape(String) This method escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with the escape codes. This instructs a regular expression engine to interpret these characters literally rather than as metacharacters.
    GetGroupNames() This method returns an array of capturing the group names for the regular expression.
    GetGroupNumbers() This method returns an array of capturing group numbers that correspond to group names in the array.
    GetHashCode() This method serves as a default hash function.
    GetType() This method gets the Type of a current instance.
    GroupNameFromNumber(Int32) This method gets the group name that corresponds to the particular group number.
    InitializeReferences() This method is used by a Regex object created by the CompileToAssembly method.
    IsMatch(String) This method denotes whether the regular expression particular in the Regex constructor finds a match in a specified input string.
    IsMatch(String, String, RegexOptions) This method denotes whether the specified regular expression finds a match in the specified input string, using the specified matching options.
    Match(String) This method finds the specified input string for the first occurrence of the regular expression specified in the Regex constructor.
    Match(String, Int32) This method searches the input string for the first occurrence of a regular expression, beginning at the specified starting position in a string.
    Matches(String) This method finds the specified input string for all occurrences of the regular expression.
    Matches(String, Int32) This method finds the particular input string for all occurrences of a regular expression, beginning at the specified starting position in a string.
    MemberwiseClone() This method generates a shallow copy of the current Object.
    Replace(String, MatchEvaluator) This method replaces all the strings that match a specified regular expression with a string returned by a MatchEvaluator delegate.
    Replace(String, MatchEvaluator, Int32) This method replaces a specified maximum number of strings that match a regular expression pattern with the string returned by the MatchEvaluatordelegate.
    Split(String) This method splits an input string into an array of substrings at the positions explained by a regular expression pattern specified in the Regex constructor.
    Split(String, Int32) This method splits an input string a specified maximum number of times into an array of substrings, at the positions explained by a regular expression specified in the Regex constructor.
    ToString() This method returns the regular expression pattern that was passed into a Regexconstructor.
    Unescape(String) This method converts any escaped characters in an input string.
    UseOptionC() This method is used by a Regex object created by the CompileToAssembly method.
    UseOptionR() This method is used by a Regex object created by the CompileToAssembly method.
    ValidateMatchTimeout(TimeSpan) This method checks a time-out interval is within an acceptable range.

Are you looking training with Right Jobs?

Contact Us

Popular Courses