class System::Text::Regex

A regular expression class based on the PCRE project (Perl Compatible Regular Expressions) http://www.pcre.org

Constructors

Regex (const string)Constructs a new Regex object from the given regular expression string.
Regex (const string, const string)Constructs a new Regex object from a regular expression string and a string of flags.

Convertors

string convertor ()Converts the regular expression of this Regex object back to a string.

Methods

int AnyMatch (const string)Checks if the specified text contains any match for this regular expression.
int ExactMatch (const string)Checks if the specified text is an exact match for this regular expression.
Match Matches (const string)Checks if the specified text is an exact match for this regular expression.
string Replace (const string, const string)Replaces all matches in the specified text by the specified replace string.
Match Search (const string)Returns the first match of the regular expression in the specified text.
Match Search (const string, const int, const int)Returns the first match in the specified portion of the text.
Match[] Search (const string)Returns an array of all matches to this regular expression in the specified text.
string[] Search (const string, const string)Returns a string array of all matches in the specified text.
Search (const string, Enumerator, var)Calls the specified delegate for every match in the text.
string[] Split (const string, const int)Interprets this regular expression as a delimiter and splits the given text into substrings.

Properties

string Error ()Returns a string describing the error if the regular expression is invalid.
int Valid ()Returns true if this regular expression object is valid, false if the object was constructed with a bad regular expression.

Type Declaration

native class System::Text::Regex
{
    ["A regular expression class based on the PCRE project (Perl Compatible Regular Expressions) http://www.pcre.org"]
    delegate Enumerator (Match match, var args); ["Delegate type for functions that can be passed into the Regex::Search() method."]
    method Regex (const string expr); ["Constructs a new Regex object from the given regular expression string."]
    method Regex (const string expr, const string flags); ["Constructs a new Regex object from a regular expression string and a string of flags. The following flags are valid: <ul><li>i - Perform case insensitive search</li><li>m - Enable searching multi-line text</li><li>s - The dot metacharacter will also match newline characters</li><li>U - Perform an ungreedy search</li><li>u - Enable support for UTF-8 strings</li><li>x - Ignore white space characters in the regular expression (except in character classes)</li></ul>All invalid characters in the flags string are silently ignored."]
    method string convertor (); ["Converts the regular expression of this Regex object back to a string."]
    method int ExactMatch (const string text); ["Checks if the specified text is an exact match for this regular expression. No search in the text is performed. Returns true if the text is a match."]
    method int AnyMatch (const string text); ["Checks if the specified text contains any match for this regular expression. Searches the text for a match. Returns true if at least one match was found."]
    method Match Matches(const string text); ["Checks if the specified text is an exact match for this regular expression. If the text matches the regular expression, a Match object is returned. Otherwise this method returns null."]
    method Match Search (const string text); ["Returns the first match of the regular expression in the specified text. If no match is found, null is returned."]
    method Match Search (const string text, const int start, const int length); ["Returns the first match in the specified portion of the text. If the text in that section does not contain a match, returns null."]
    method Match[] Search (const string text); ["Returns an array of all matches to this regular expression in the specified text. If no match is found at all, an array with zero length is returned."]
    method string[] Search (const string text, const string format); ["Returns a string array of all matches in the specified text. If no match is found at all, an array with zero length is returned. If 'format' is not null, matches will be formatted before being added to the array. The format string can contain references to sub-matches ($1 - $9). If 'format' is null, matches will be added verbatim to the array."]
    method Search (const string text, Enumerator fn, var args); ["Calls the specified delegate for every match in the text. A Match object is passed to the delegate to allow examining the match's properties."]
    method string[] Split (const string text, const int discard); ["Interprets this regular expression as a delimiter and splits the given text into substrings. The returned array will contain all text between delimiters, but not the delimiters itself. If 'discard' is 'false', a delimiter directly following another delimiter may produce an empty string element in the array. If set to 'true', empty strings will not be added to the array. If 'text' does not match the delimeter at all, the resulting array will contain a single element, which is a copy of 'text'."]
    method string Replace (const string text, const string replace); ["Replaces all matches in the specified text by the specified replace string. The replace string can contain references to sub-matches ($1 - $9)."]
    accessor int Valid (); ["Returns true if this regular expression object is valid, false if the object was constructed with a bad regular expression."]
    accessor string Error (); ["Returns a string describing the error if the regular expression is invalid."]
}

Reference

method Regex (const string expr)

Constructs a new Regex object from the given regular expression string.


method Regex (const string expr, const string flags)

Constructs a new Regex object from a regular expression string and a string of flags.

The following flags are valid:

  • i - Perform case insensitive search
  • m - Enable searching multi-line text
  • s - The dot metacharacter will also match newline characters
  • U - Perform an ungreedy search
  • u - Enable support for UTF-8 strings
  • x - Ignore white space characters in the regular expression (except in character classes)
All invalid characters in the flags string are silently ignored.


method string convertor ()

Converts the regular expression of this Regex object back to a string.


method int AnyMatch (const string text)

Checks if the specified text contains any match for this regular expression.

Searches the text for a match. Returns true if at least one match was found.


method int ExactMatch (const string text)

Checks if the specified text is an exact match for this regular expression.

No search in the text is performed. Returns true if the text is a match.


method Match Matches (const string text)

Checks if the specified text is an exact match for this regular expression.

If the text matches the regular expression, a Match object is returned. Otherwise this method returns null.


method string Replace (const string text, const string replace)

Replaces all matches in the specified text by the specified replace string.

The replace string can contain references to sub-matches ($1 - $9).


method Match Search (const string text)

Returns the first match of the regular expression in the specified text.

If no match is found, null is returned.


method Match Search (const string text, const int start, const int length)

Returns the first match in the specified portion of the text.

If the text in that section does not contain a match, returns null.


method Match[] Search (const string text)

Returns an array of all matches to this regular expression in the specified text.

If no match is found at all, an array with zero length is returned.


method string[] Search (const string text, const string format)

Returns a string array of all matches in the specified text.

If no match is found at all, an array with zero length is returned. If 'format' is not null, matches will be formatted before being added to the array. The format string can contain references to sub-matches ($1 - $9). If 'format' is null, matches will be added verbatim to the array.


method Search (const string text, Enumerator fn, var args)

Calls the specified delegate for every match in the text.

A Match object is passed to the delegate to allow examining the match's properties.


method string[] Split (const string text, const int discard)

Interprets this regular expression as a delimiter and splits the given text into substrings.

The returned array will contain all text between delimiters, but not the delimiters itself. If 'discard' is 'false', a delimiter directly following another delimiter may produce an empty string element in the array. If set to 'true', empty strings will not be added to the array. If 'text' does not match the delimeter at all, the resulting array will contain a single element, which is a copy of 'text'.


accessor string Error ()

Returns a string describing the error if the regular expression is invalid.


accessor int Valid ()

Returns true if this regular expression object is valid, false if the object was constructed with a bad regular expression.