A Regular Expression (regex) is a sequence of characters that define a pattern that allows you to search, match, locate, replace, manipulate, and manage text. I explained the basics of Regular Expressions in an earlier article: What is a Regular Expression?
Alternation allows allows a match with one or more patterns in a regular expression. The alternate patterns are separated by a pipe character referred to the alternation operator. The syntax for alternation is shown below.
In the example below, the name of one or more programming languages in the sentence is matched and displayed in a message box.
In the example below, the names of the kinds of pets in the sentence is matched and displayed in a message box.
The regular expression defines an alternation containing the words cats, dogs, fish, and birds. Given the g (global) control character, the alternation regular expression will return, in an array, all the matches it finds to every alternate pattern.
This example displays "cats,dogs,fish,birds" in a message box.
In this article you learned how to create an alternation regular expression that will match alternate patterns in a string.
Learn more at amazon.com