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
stringhe 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
stringThe regular expression pattern to compile.
matchFlags
RegexOptionsThe 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
Options
The Match Options of this Regular Expression
public RegexOptions Options { get; set; }
Property Value
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
stringThe 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
stringstring 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
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
stringString 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
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
Returns
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
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
stringThe string to match against
pattern
stringThe pattern to match
options
RegexOptionsRegexOptions 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
stringThe 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
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
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
stringThe string to match against
pattern
stringThe pattern to match
options
RegexOptionsRegexOptions 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
stringString to substitute within
substitution
stringString 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
stringString to substitute within
substitution
stringString to substitute for matches of this regular expression
maxOccurances
intMaximum number of occurrences
start
intStart 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
stringString 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
stringString to split on this regular exression
maxMatches
intThe 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
stringString to split on this regular expression.
maxMatches
intThe maximum number of matches to split.
start
intThe offset in the string to start at.
length
intThe 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
stringThe pattern to match against
split
stringThe string to split
options
RegexOptionsThe 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
stringThe pattern to match against
options
RegexOptionsThe options to utilize during matching
split
stringThe string to split
start
intthe start position in the input
maxOccurances
intthe 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
RegexThe Regex to use to Split the input string
input
stringThe input string
count
intThe amount of Splits to perform
startat
intThe 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
stringThe 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