BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Regular Expressions
.NET 1.1+

Basic Matching with .NET Regular Expressions

The second part of the Regular Expressions in .NET tutorial looks at the basic matching features of the regular expressions engine. This article describes several Regex matching methods and how to search for literal strings.

Matching Literals

Regular expressions are ideally suited to validating that a string matches a given pattern or finding the parts of a string that match the pattern. In almost every case, the pattern includes control characters, which mean that the located matches will not be identical to the pattern itself.

It is unusual to use regular expressions to find literal strings. However, in some cases you may need to, and searching for literals is a useful way to demonstrate the matching methods supplied by the .NET regular expression engine. In this article we'll see three key matching methods of the Regex class, searching for literals in all cases.

IsMatch Method

The IsMatch method is the most basic matching option and is ideal for validation of user input. It compares an input string with a regular expression pattern and returns true if the input string contains a match, and false otherwise. IsMatch can be called as a static method or an instance method. In this article we'll only look at the static option.

The basic IsMatch method has two parameters. The first receives a string containing the text to be searched. The second is a string containing the pattern. As we are using literal strings for the regular expression, the following code simply detects whether the input string contains the text of the second argument.

string input = "The quick brown fox jumps over the dog.";

bool isMatch1 = Regex.IsMatch(input, "brown");  // true
bool isMatch2 = Regex.IsMatch(input, "black");  // false

Match Method

The Match method is used in a similar manner to IsMatch. However, rather than returning a Boolean value, it yields a Match object that contains details about the matched text. This includes the actual text found, which usually differs from the pattern, the starting index of the matched text within the input string and the length of the matched text.

For example, the following code matches the literal text, "brown", within the input string's sentence. The call to Console.WriteLine shows the index of the match, the text found and its length.

string input = "The quick brown fox jumps over the brown dog.";

Match match = Regex.Match(input, "brown");
Console.WriteLine("{0}: '{1}' {2} chars", match.Index, match.Value, match.Length);

/* OUTPUT

10: 'brown' 5 chars

*/

If no matching text can be found, the return value matches the static property, Match.Empty. This object's Success property is set to false, as shown below:

string input = "The quick brown fox jumps over the brown dog.";

Match match = Regex.Match(input, "black");
bool matched = match.Success;              // false
bool notAMatch = match == Match.Empty;     // true

Finally, the Match object includes a method called NextMatch. This allows you to find the next piece of text that matches the regular expression. As before, this returns a Match object. It is Match.Empty once the matches have been exhausted.

The following sample finds all instances of the text, "brown", within the input string.

string input = "The quick brown fox jumps over the brown dog.";

Match match = Regex.Match(input, "brown");
while (match.Success)
{
    Console.WriteLine("{0}: '{1}' {2} chars", match.Index, match.Value, match.Length);
    match = match.NextMatch();
}

/* OUTPUT

10: 'brown' 5 chars
35: 'brown' 5 chars

*/

Matches Method

If you need to find all elements of the input string that fit the pattern defined by a regular expression, you can call the Matches method. This processes the entire string and returns a collection of Match objects in a MatchCollection instance.

The code below has similar functionality to the previous example. As the collection is populated in one operation, the sample also outputs the number of matches, using the collection's Count property.

string input = "The quick brown fox jumps over the brown dog.";

MatchCollection matches = Regex.Matches(input, "brown");
Console.WriteLine("{0} match(es)", matches.Count);
foreach (Match match in matches)
{
    Console.WriteLine("{0}: '{1}' {2} chars", match.Index, match.Value, match.Length);
}

/* OUTPUT

2 match(es)
10: 'brown' 5 chars
35: 'brown' 5 chars

*/
2 September 2015