How to use Quantifier "?" in Regular Expression.
Quantifier "?" in Regular Expression?
Explanation
The "?" metacharacter otherwise known as quantifier is used to find the character or a sequence of characters that may or may not occur.
For example if we have a pattern "lt?" it matches the string that has the letter "l" that may have a "t" or may not have a "t" followed by it. So the string matched would be "l","lt","looo", "looot" etc.
PHP Example:
<?php
$name = "ae";
if (preg_match("/a(dca)?e/", $name))
{echo "Pattern matches!";}
else
{echo "Pattern not matched!";}
?>
Result :
Pattern matches!
In the above example possible matches can be "ae" or "adcae" only as "dca" is grouped using a "()".
Perl Example:
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
if ("looo" =~ m/lt?/)
{print "The pattern matched.!n";}
else
{print "The pattern not found!n";}
Result :
The pattern matched.!
In the above example's the pattern "lt?" means that the string should starts with "l" but doesnt have have a "t" following, even then the pattern is matched as "?" is used which means the preceding character "t" may occur or may not.
Non-greedy Pattern Matching:
This type of matching can be done using "?" along with the quantifiers "*", "?", "*" and "{}" to get the minimum matched characters.
Perl Example:
$str= "exagerate";
if ($str =~ /e(.*)e/)
{print "The output from a Greedy Match::$1";}
print "<br>";
if ($str =~ /e(.*?)e/)
{print "The output from a Non Greedy Match::$1";}
Result:
The output from a Greedy Match::xagerat
The output from a Non Greedy Match::xag
PHP Example:
$name = "exagerate";
preg_match("/e(.*)e/",$name,$matches);
print "The Greedy Matched characters::";
echo $matches[1];
print "<br>";
preg_match("/e(.*?)e/",$name,$matches);
print "The Non Greedy Matched characters::";
echo $matches[1];
Result:
The Greedy Matched characters::xagerat
The Non Greedy Matched characters::xag
In the above examples asyou can see in the second example that characters between the first and the next occurence of "e" is displayed and not the last "e".