use strict;
my $str = '<ol>kjkjkj
</ol>
<ol s>
<li>Table of Contents.....................CT.TS</li>
<li>Gamecube Controls.....................CN.GC</li>
<li>Nintendo 64 Controls..................CN.64</li>
<li>Walkthrough...........................01.00
<ol>
<li>Kokiri Forest...............01.01</li>
</ol>
</li>
<li>Sidequests & Minigames................02.00
<ol>
<li>Shooting Gallery............02.01</li>
</ol>
</li>
<li>Boss Guide............................03.00
<ol>
<li>Gohma.......................03.01</li>
<li>Final Boss [Second Form]....03.15</li>
</ol>
</li>
<li>Item Checklist........................04.00
<ol>
<li>Final Dungeon...............04.47</li>
</ol>
</li>
<li>Shop Inventory........................05.00
<ol>
<li>Kokiri Shop [Young].........05.01</li>
<li>Goron Shop [Adult]..........05.09</li>
</ol>
</li>
<li>Ocarina Notes.........................06.00
<ol><ol>sdsds</ol>
<li>Requ<ol></ol>iem of Spirit...........06.12</li>
</ol>
</li>
<li>Heart Containers......................07.00</li>
<li>Item List.............................15.00</li>
<li>Legal & Copyright.....................LE.AL</li>
<li>Credits & Thanks......................CR.DS</li>
</ol>
<ol><ol><ol><ol><ol></ol></ol></ol></ol></ol>
sdsd
k<ol>jlkjlkj
kjbkjh</ol> ';
my $regex = qr/\<(ol)(?: .*?)?\>(?:[^\<]|\<(.*?)\>.*\<\/\2\>)*\<\/\1\>/sp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html