Table of Contents

Class Regex

Namespace
System.Text.RegularExpressions
Assembly
System.Text.RegularExpressions.dll

A lightweight Regular Expression Engine and Parser. Contributed by Julius Friedman

public sealed class Regex
Inheritance
Regex
Inherited Members
Extension Methods

Constructors

Regex()

Internal use only. Constructs a regular expression matcher with no initial program. This is likely to be an uncommon practice, but is still supported.

public Regex()

Regex(string)

Constructs a regular expression matcher from a String by compiling it using a new instance of RECompiler. If you will be compiling many expressions, you may prefer to use a single RECompiler object instead.

public Regex(string pattern)

Parameters

pattern string

he regular expression pattern to compile.

Regex(string, RegexOptions)

Constructs a regular expression matcher from a String by compiling it using a new instance of RECompiler. If you will be compiling many expressions, you may prefer to use a single RECompiler object instead.

public Regex(string pattern, RegexOptions matchFlags)

Parameters

pattern string

The regular expression pattern to compile.

matchFlags RegexOptions

The MatchOptions

Properties

CacheSize

The size of the Regex Cache indicating how many RegexProgram's will be stored in the Stack for later use.

public static int CacheSize { get; set; }

Property Value

int

Options

The Match Options of this Regular Expression

public RegexOptions Options { get; set; }

Property Value

RegexOptions

Methods

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.

public static string Escape(string input)

Parameters

input string

The input string to escape

Returns

string

The escaped input string

GetMatches(string)

Returns an array of Strings, whose ToString representation matches a regular expression. This method works like the Perl function of the same name. Given a regular expression of "a*b" and an array of String objects of [foo, aab, zzz, aaaab], the array of Strings returned by grep would be [aab, aaaab].

public string[] GetMatches(string search)

Parameters

search string

string to search

Returns

string[]

Array of Strings whose value matches this regular expression

GetMatches(string[])

Returns an array of Strings, whose ToString representation matches a regular expression. This method works like the Perl function of the same name. Given a regular expression of "a*b" and an array of String objects of [foo, aab, zzz, aaaab], the array of Strings returned by grep would be [aab, aaaab].

public string[] GetMatches(string[] search)

Parameters

search string[]

Array of string to search

Returns

string[]

Array of Strings whose value matches this regular expression

GetMatches(string[], int, int)

Returns an array of Strings, whose ToString representation matches a regular expression. This method works like the Perl function of the same name. Given a regular expression of "a*b" and an array of String objects of [foo, aab, zzz, aaaab], the array of Strings returned by grep would be [aab, aaaab].

public string[] GetMatches(string[] search, int start, int length)

Parameters

search string[]

Array of string to search

start int

Start index

length int

The length

Returns

string[]

Array of Strings whose value matches this regular expression

IsMatch(string)

Matches the current regular expression program against a String.

public bool IsMatch(string search)

Parameters

search string

String to match against

Returns

bool

True if search string matched

IsMatch(string, int)

Matches the current regular expression program against a character array,starting at a given index.

public bool IsMatch(string search, int i)

Parameters

search string

String to match against

i int

Index to start searching at

Returns

bool

True if string matched

Match(string)

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

public Match Match(string input)

Parameters

input string

Returns

Match

A Match

Match(string, int)

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

public Match Match(string input, int startat)

Parameters

input string
startat int

Returns

Match

Match(string, string)

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

public static Match Match(string input, string pattern)

Parameters

input string

The string to match against

pattern string

The pattern to match

Returns

Match

A Match representing the first match from the input against the pattern

Match(string, string, RegexOptions)

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

public static Match Match(string input, string pattern, RegexOptions options)

Parameters

input string

The string to match against

pattern string

The pattern to match

options RegexOptions

RegexOptions to utilize during the Match process

Returns

Match

A Match representing the first match from the input against the pattern with the specified options

Matches(string)

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

public MatchCollection Matches(string input)

Parameters

input string

The input string to match against

Returns

MatchCollection

A Collection of Matches from the input

Matches(string, int)

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

public MatchCollection Matches(string input, int startat)

Parameters

input string

The string to match against

startat int

The position to start matching at

Returns

MatchCollection

A Collection of Matches from the input

Matches(string, string)

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

public static MatchCollection Matches(string input, string pattern)

Parameters

input string

The string to match against

pattern string

The pattern to match

Returns

MatchCollection

A Collection of Matches from the input

Matches(string, string, RegexOptions)

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

public static MatchCollection Matches(string input, string pattern, RegexOptions options)

Parameters

input string

The string to match against

pattern string

The pattern to match

options RegexOptions

RegexOptions to utilize during the Match process

Returns

MatchCollection

A Collection of Matches from the input

Replace(string, string)

Substitutes a string for this regular expression in another string. This method works like the Perl function of the same name. Given a regular expression of "a*b", a String to substituteIn of "aaaabfooaaabgarplyaaabwackyb" and the substitution String "-", the resulting String returned by Substring would be "-foo-garply-wacky-".

public string Replace(string substituteIn, string substitution)

Parameters

substituteIn string

String to substitute within

substitution string

String to substitute for all matches of this regular expression.

Returns

string

The string substituteIn with zero or more occurrences of the current regular expression replaced with the substitution String (if this regular expression object doesn't match at any position, the original String is returned unchanged).

Replace(string, string, int, int)

Substitutes a string for this regular expression in another string. This method works like the Perl function of the same name. Given a regular expression of "a*b", a String to substituteIn of "aaaabfooaaabgarplyaaabwackyb" and the substitution String "-", the resulting String returned by subst would be "-foo-garply-wacky-".

It is also possible to reference the contents of a parenthesized expression with $0, $1, ... $9. A regular expression of "http://[\\.\\w\\-\\?/~_@&=%]+", a String to substituteIn of "visit us: http://www.apache.org!" and the substitution String "<a href=\"$0\">$0</a>", the resulting String returned by subst would be "visit us: <a href=\"http://www.apache.org\">http://www.apache.org</a>!".

Note: $0 represents the whole match.
public string Replace(string substituteIn, string substitution, int maxOccurances, int start)

Parameters

substituteIn string

String to substitute within

substitution string

String to substitute for matches of this regular expression

maxOccurances int

Maximum number of occurrences

start int

Start index

Returns

string

The string substituteIn with zero or more occurrences of the current regular expression replaced with the substitution String (if this regular expression object doesn't match at any position, the original String is returned unchanged).

Split(string)

Splits a string into an array of strings on regular expression boundaries. This function works the same way as the Perl function of the same name. Given a regular expression of "[ab]+" and a string to split of "xyzzyababbayyzabbbab123", the result would be the array of Strings "[xyzzy, yyz, 123]".

Please note that the first string in the resulting array may be an empty string. This happens when the very first character of input string is matched by the pattern.

public string[] Split(string s)

Parameters

s string

String to split on this regular exression

Returns

string[]

Array of strings

Split(string, int)

Splits a string into an array of strings on regular expression boundaries. This function works the same way as the Perl function of the same name. Given a regular expression of "[ab]+" and a string to split of "xyzzyababbayyzabbbab123", the result would be the array of Strings "[xyzzy, yyz, 123]".

Please note that the first string in the resulting array may be an empty string. This happens when the very first character of input string is matched by the pattern.

public string[] Split(string s, int maxMatches)

Parameters

s string

String to split on this regular exression

maxMatches int

The maximum number of matches to split

Returns

string[]

Array of strings

Split(string, int, int, int)

Splits a string into an array of strings on regular expression boundaries. This function works the same way as the Perl function of the same name. Given a regular expression of "[ab]+" and a string to split of "xyzzyababbayyzabbbab123", the result would be the array of Strings "[xyzzy, yyz, 123]".

Please note that the first string in the resulting array may be an empty string. This happens when the very first character of input string is matched by the pattern.

public string[] Split(string s, int maxMatches, int start, int length)

Parameters

s string

String to split on this regular expression.

maxMatches int

The maximum number of matches to split.

start int

The offset in the string to start at.

length int

The maximum length in the string that should parsed.

Returns

string[]

Array of strings

Split(string, string, RegexOptions)

Splits the input string at the positions defined by a specified regular expression pattern. Specified options modify the matching operation.

public static string[] Split(string pattern, string split, RegexOptions options)

Parameters

pattern string

The pattern to match against

split string

The string to split

options RegexOptions

The options to utilize during matching

Returns

string[]

The result of splitting the input string against the pattern

Split(string, RegexOptions, string, int, int)

Splits the input string at the positions defined by a specified regular expression pattern. Specified options modify the matching operation.

public static string[] Split(string pattern, RegexOptions options, string split, int start, int maxOccurances)

Parameters

pattern string

The pattern to match against

options RegexOptions

The options to utilize during matching

split string

The string to split

start int

the start position in the input

maxOccurances int

the maximum amount of splits to perform

Returns

string[]

Split(Regex, string, int, int)

Splits the input string at the positions defined by a specified regular expression pattern. Specified options modify the matching operation.

public static string[] Split(Regex regex, string input, int count, int startat)

Parameters

regex Regex

The Regex to use to Split the input string

input string

The input string

count int

The amount of Splits to perform

startat int

The start index in the input

Returns

string[]

The result of splitting the input string against the Regexp

ToFullRegularExpression(string)

Converts a 'simplified' regular expression to a full regular expression

public static string ToFullRegularExpression(string simplePattern)

Parameters

simplePattern string

The pattern to convert

Returns

string

The full regular expression

ToString()

Provides a string representation of the pattern this Regular Expression is matching

public override string ToString()

Returns

string

The Pattern of this Regular Expression