Name

Regex

Synopsis

This class represents a regular expression. Use it to search for patterns in string data. It provides static methods that search for a pattern without explicitly creating Regex instances as well as instance methods that allow you to interact with a Regex object.

The various static methods employed by Regex take the input string to search for as the first argument and the regular expression pattern string as the second. This is equivalent to constructing a Regex instance with a pattern string, using it, and destroying it immediately. Most methods are overloaded as instance methods as well. These do not require a pattern argument, as this is provided with the constructor.

The Match() and Matches() methods search an input string for a single match or all matches. Their overloads are the same. The first argument is the input string. You can specify which position in the string the search should start at using a second integer parameter. Match() also lets you specify the length of substring to search after that position. IsMatch() works the same way as Match(), except that it returns a boolean indicating whether the string contains a match.

The Split() method acts like the System.String.Split() method. It uses the Regex pattern as a delimiter to split the input string into an array of substrings. (The delimiter is not included in the substrings.) You can provide a maximum number of substrings to return, in which case the last substring returned is the remainder of ...

Get C# in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.