Backtracking in Regular Expression

What is Backtracking in Regular Expression?


Storing the matching patterns for future use is known as Backtracking.

Example :

#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string = '17-12-76 12:30:15';
if ($string =~m/([0-9]+-[0-9]+-[0-9]+) ([[0-9]+:[0-9]+:[0-9]+)/)
print "The Date of Birth is '$1'.n";
print "<br>";
print "The Date is '$2'.n";
Result :

The Date of Birth is '17-12-76'.
The Date is '12:30:15'.

In the above example, we have two matching patterns one for the date, then for a time, and the values returned when there is a match can be referred anywhere using "$1" for the first pattern, "$2" for the second pattern.

Ask Questions

Ask Question