Regular Expression





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Regex Topics
Introduction Introduction
Common Metacharacter Common Metacharacter's
Metacharacters Metacharacter's
Qualifiers Quantifier's
Word Boundry Word Boundry
Non-word Boundry Non-word Boundry
POSIX Character Classes POSIX-Character Classes
Feedback Feedback
Forums Ask Your Doubts
 




Regular Expression Meta-character Alternation "|"


Tutorials Regular-expression

Topic

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:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    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.





A Note

Simple Regex Regular Expression Tutorial Online. We welcome your Valuable feedbacks or suggestions. This is a copyright content.


Other Links

web hosting