Regular Expression Meta-character Alternation "|"
What is regular expression meta-character Alternation "|"?
Explanation
/prob|n|r|l|ate/
In Regular Expression, the meta-character alternation "|" or pipe is used to match character seperated with it from left to right, it just like a "OR" operator.
PHP Example:
<?php
$name = "bit";
if (preg_match("/c(a|i|u)t/", $name))
{echo "Regex Pattern matches!";}
else
{echo "Regex Pattern not matched!";}
?>
Result :
The Regex Pattern not matched!
In the above example "aiu" is similar to "a|i|u", so the possible matches would be "cat","cit","cut", since the string is "b" its unmatched.
Perl Example:
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
if ("AS235452" =~ m/([a-z]{2}|[0-9]{5})/)
{print "The Regex pattern matched.!n";}
else
{print "The Regex pattern not found!n";}
Result :
The pattern matched.!
In the above example's the pattern "([A-Z]{2}|[0-9]{5})" matches a string only if it has "5" numbers or "2" uppercase alphabets.But the string "AS235452" has only two upper case letters and since "|" is used the second condition is is matched that is for "5" numbers.