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+

Regular Expression Alternation

The sixth part of the Regular Expressions in .NET tutorial continues to examine the regular expression language. This article describes alternation, which allows a single regular expression to match multiple patterns.

Alternation

So far in the regular expressions tutorial we've worked with regular expressions that match a single pattern. In some cases, you might want to match one of multiple patterns. You could perform several matches and combine the results. However, using alternation allows you to search for multiple regular expression patterns with a single operation.

To use alternation, you use the pipe character (|) to delimit a series of patterns. You can include as many patterns as you wish. For example, the following code matches either "dog" or "fox" in the input string:

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

foreach (Match match in Regex.Matches(input, @"dog|fox"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match.Value, match.Index);
}

/* OUTPUT
    
Matched 'fox' at index 16
Matched 'dog' at index 40
             
*/

The sample code above looks for patterns containing only literal characters. However, you can include any regular expression language items within each of the patterns. For example, the updated version below matches three patterns. The first is "d.g", which matches any three-character sequence that starts with 'd' and ends with 'g'. The second pattern looks for an 'f', followed by any single character and an 'x'. The third pattern uses the word character class and word boundary anchors to find four-letter words.

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

foreach (Match match in Regex.Matches(input, @"d.g|f.x|\b\w\w\w\w\b"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match.Value, match.Index);
}

/* OUTPUT
    
Matched 'fox' at index 16
Matched 'over' at index 26
Matched 'lazy' at index 35
Matched 'dog' at index 40
            
*/

Alternation within a Larger Expression

Alternation is useful when you wish to find several patterns. It can also be used within a regular expression, where part of the pattern must match one of a set of options. For example, you might wish to find the words, "dog", "fox" or "jump". If you used the expression, "dog|fox|jump", you would match those words. However, you would also find matches where any of those words appeared within a larger word. You could add word boundary anchors to all three words or, as in the following example, you could contain the alternation items within parentheses:

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

foreach (Match match in Regex.Matches(input, @"\b(dog|fox|jump)\b"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match.Value, match.Index);
}

/* OUTPUT
    
Matched 'fox' at index 16
Matched 'dog' at index 40
            
*/

NB: Parentheses can be used in many situations where a group of characters should be processed as a single item. We'll see them used again later in this tutorial.

25 September 2015