Regex Class

Definition

Represents an immutable regular expression. Also contains static methods that allow use of regular expressions without instantiating a Regex explicitly.

public ref class Regex
public ref class Regex : System::Runtime::Serialization::ISerializable
public class Regex
public class Regex : System.Runtime.Serialization.ISerializable
[System.Serializable]
public class Regex : System.Runtime.Serialization.ISerializable
type Regex = class
type Regex = class
    interface ISerializable
[<System.Serializable>]
type Regex = class
    interface ISerializable
Public Class Regex
Public Class Regex
Implements ISerializable
Inheritance
Regex
Derived
Attributes
Implements

Remarks

The Regex class represents the .NET regular expression engine. It can be used to quickly parse large amounts of text to find specific character patterns; to extract, edit, replace, or delete text substrings; and to add the extracted strings to a collection to generate a report.

Constructors

Name Description
Regex()

Initializes a new instance of the Regex class.

Regex(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the Regex class by using serialized data.

Regex(String, RegexOptions, TimeSpan)

Initializes a new instance of the Regex class for the specified regular expression, with options that modify the pattern and a value that specifies how long a pattern matching method should attempt a match before it times out.

Regex(String, RegexOptions)

Initializes a new instance of the Regex class for the specified regular expression, with options that modify the pattern.

Regex(String)

Initializes a new instance of the Regex class for the specified regular expression.

Fields

Name Description
capnames

When named captures are used, maps capture names to their corresponding index. Otherwise, null.

caps

When captures are sparse, maps capture numbers to their corresponding index in the capture array. Otherwise, null.

capsize

The number of capturing groups defined in the regular expression pattern.

capslist

When captures are sparse or named captures are used, contains the sorted list of capture names. Otherwise, null.

factory

A factory used to create RegexRunner instances for executing the regular expression.

InfiniteMatchTimeout

Specifies that a pattern-matching operation should not time out.

internalMatchTimeout

The maximum amount of time that can elapse in a pattern-matching operation before the operation times out.

pattern

The regular expression pattern that was passed to the constructor.

roptions

The regular expression options that were passed to the constructor.

Properties

Name Description
CacheSize

Gets or sets the maximum number of entries in the current static cache of regular expression instances.

CapNames

Gets or sets a dictionary that maps named capturing groups to their index values.

Caps

Gets or sets a dictionary that maps numbered capturing groups to their index values.

MatchTimeout

Gets the time-out interval of the current instance.

Options

Gets the options that were passed into the Regex constructor.

RightToLeft

Gets a value that indicates whether the regular expression searches from right to left.

Methods

Name Description
CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)
Obsolete.

Compiles one or more specified Regex objects and a specified resource file to a named assembly with the specified attributes.

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])
Obsolete.

Compiles one or more specified Regex objects to a named assembly with the specified attributes.

CompileToAssembly(RegexCompilationInfo[], AssemblyName)
Obsolete.

Compiles one or more specified Regex objects to a named assembly.

Count(ReadOnlySpan<Char>, Int32)

Searches an input span for all occurrences of a regular expression and returns the number of matches.

Count(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

Searches an input span for all occurrences of a regular expression and returns the number of matches.

Count(ReadOnlySpan<Char>, String, RegexOptions)

Searches an input span for all occurrences of a regular expression and returns the number of matches.

Count(ReadOnlySpan<Char>, String)

Searches an input span for all occurrences of a regular expression and returns the number of matches.

Count(ReadOnlySpan<Char>)

Searches an input span for all occurrences of a regular expression and returns the number of matches.

Count(String, String, RegexOptions, TimeSpan)

Searches an input string for all occurrences of a regular expression and returns the number of matches.

Count(String, String, RegexOptions)

Searches an input string for all occurrences of a regular expression and returns the number of matches.

Count(String, String)

Searches an input string for all occurrences of a regular expression and returns the number of matches.

Count(String)

Searches an input string for all occurrences of a regular expression and returns the number of matches.

EnumerateMatches(ReadOnlySpan<Char>, Int32)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.

EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.

EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.

EnumerateMatches(ReadOnlySpan<Char>, String)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.

EnumerateMatches(ReadOnlySpan<Char>)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.

EnumerateSplits(ReadOnlySpan<Char>, Int32, Int32)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueSplitEnumerator to iterate over the splits around matches.

EnumerateSplits(ReadOnlySpan<Char>, Int32)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueSplitEnumerator to iterate over the splits around matches.

EnumerateSplits(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueSplitEnumerator to iterate over the splits around matches.

EnumerateSplits(ReadOnlySpan<Char>, String, RegexOptions)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueSplitEnumerator to iterate over the splits around matches.

EnumerateSplits(ReadOnlySpan<Char>, String)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueSplitEnumerator to iterate over the splits around matches.

EnumerateSplits(ReadOnlySpan<Char>)

Searches an input span for all occurrences of a regular expression and returns a Regex.ValueSplitEnumerator to iterate over the splits around matches.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Escape(String)

Escapes a minimal set of characters (, *, +, ?, |, {, [, (, ), ^, $, ., #, and white space) by replacing them with their escape codes. This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.

Finalize()

This member overrides Finalize(), and more complete documentation might be available in that topic.

Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.

GetGroupNames()

Returns an array of capturing group names for the regular expression.

GetGroupNumbers()

Returns an array of capturing group numbers that correspond to group names in an array.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
GroupNameFromNumber(Int32)

Gets the group name that corresponds to the specified group number.

GroupNumberFromName(String)

Returns the group number that corresponds to the specified group name.

InitializeReferences()
Obsolete.

Used by a Regex object generated by the CompileToAssembly(RegexCompilationInfo[], AssemblyName) method. This method is obsolete.

IsMatch(ReadOnlySpan<Char>, Int32)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input span, starting at the specified position.

IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options and time-out interval.

IsMatch(ReadOnlySpan<Char>, String, RegexOptions)

Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options.

IsMatch(ReadOnlySpan<Char>, String)

Indicates whether the specified regular expression finds a match in the specified input span.

IsMatch(ReadOnlySpan<Char>)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input span.

IsMatch(String, Int32)

Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.

IsMatch(String, String, RegexOptions, TimeSpan)

Indicates whether the specified regular expression finds a match in the specified input string, using the specified matching options and time-out interval.

IsMatch(String, String, RegexOptions)

Indicates whether the specified regular expression finds a match in the specified input string, using the specified matching options.

IsMatch(String, String)

Indicates whether the specified regular expression finds a match in the specified input string.

IsMatch(String)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.

Match(String, Int32, Int32)

Searches the input string for the first occurrence of a regular expression, beginning at the specified starting position and searching only the specified number of characters.

Match(String, Int32)

Searches the input string for the first occurrence of a regular expression, beginning at the specified starting position in the string.

Match(String, String, RegexOptions, TimeSpan)

Searches the input string for the first occurrence of the specified regular expression, using the specified matching options and time-out interval.

Match(String, String, RegexOptions)

Searches the input string for the first occurrence of the specified regular expression, using the specified matching options.

Match(String, String)

Searches the specified input string for the first occurrence of the specified regular expression.

Match(String)

Searches the specified input string for the first occurrence of the regular expression specified in the Regex constructor.

Matches(String, Int32)

Searches the specified input string for all occurrences of a regular expression, beginning at the specified starting position in the string.

Matches(String, String, RegexOptions, TimeSpan)

Searches the specified input string for all occurrences of a specified regular expression, using the specified matching options and time-out interval.

Matches(String, String, RegexOptions)

Searches the specified input string for all occurrences of a specified regular expression, using the specified matching options.

Matches(String, String)

Searches the specified input string for all occurrences of a specified regular expression.

Matches(String)

Searches the specified input string for all occurrences of a regular expression.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Replace(String, MatchEvaluator, Int32, Int32)

In a specified input substring, replaces a specified maximum number of strings that match a regular expression pattern with a string returned by a MatchEvaluator delegate.

Replace(String, MatchEvaluator, Int32)

In a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a string returned by a MatchEvaluator delegate.

Replace(String, MatchEvaluator)

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate.

Replace(String, String, Int32, Int32)

In a specified input substring, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

Replace(String, String, Int32)

In a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan)

In a specified input string, replaces all substrings that match a specified regular expression with a string returned by a MatchEvaluator delegate. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

Replace(String, String, MatchEvaluator, RegexOptions)

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate. Specified options modify the matching operation.

Replace(String, String, MatchEvaluator)

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate.

Replace(String, String, String, RegexOptions, TimeSpan)

In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

Replace(String, String, String, RegexOptions)

In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Specified options modify the matching operation.

Replace(String, String, String)

In a specified input string, replaces all strings that match a specified regular expression pattern with a specified replacement string.

Replace(String, String)

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.

Split(String, Int32, Int32)

Splits an input string a specified maximum number of times into an array of substrings, at the positions defined by a regular expression specified in the Regex constructor. The search for the regular expression pattern starts at a specified character position in the input string.

Split(String, Int32)

Splits an input string a specified maximum number of times into an array of substrings, at the positions defined by a regular expression specified in the Regex constructor.

Split(String, String, RegexOptions, TimeSpan)

Splits an input string into an array of substrings at the positions defined by a specified regular expression pattern. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

Split(String, String, RegexOptions)

Splits an input string into an array of substrings at the positions defined by a specified regular expression pattern. Specified options modify the matching operation.

Split(String, String)

Splits an input string into an array of substrings at the positions defined by a regular expression pattern.

Split(String)

Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.

ToString()

Returns the regular expression pattern that was passed into the Regex constructor.

Unescape(String)

Converts any escaped characters in the input string.

UseOptionC()
Obsolete.

True if the Compiled option was set.

UseOptionR()
Obsolete.

True if the RightToLeft option was set.

ValidateMatchTimeout(TimeSpan)

Validates that the specified match timeout value is valid. The valid range is TimeSpan.Zero < matchTimeout <= Regex.MaximumMatchTimeout.

Explicit Interface Implementations

Name Description
ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo object with the data necessary to deserialize the current Regex object.

Applies to

Thread Safety

The Regex class is immutable (read-only) and thread safe. Regex objects can be created on any thread and shared between threads. For more information, see Thread Safety.

See also