use strict;
my $str = ' <child name = "child_name1" type = "child_type1"> <subchild id = "1" auth="auth1">Value1</subchild> </child> <child name = "child_name2" type = "child_type2"> <subchild id = "2" auth="auth1">Value2</subchild> <subchild id = "3" auth="auth2">Value3</subchild> <subchild id = "4" auth="auth3"></subchild> <subchild id = "5" auth="auth3"/> </child>';
my $regex = qr/<(?<tagName>\w+)(([^<]*\/>)|(.*?\/(\k<tagName>)>))/mp;
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