use strict;
my $str = '<Device DevID="14212111" Model="" GroupID="">
<Block Name="ZG123CZ123" LastFileDld="" LastFullDld="">
<Group ZD="1">
<DSize ParamName="KEY" Value="RAM"/>
<DSize ParamName="REM" Value="***"/>
<DSize ParamName="*UNZIP" Value="I:ZG123CZ123.ZIP"/>
</Group>
<Group ZD="15">
<DSize ParamName="REM" Value="="***""/>
<DSize ParamName="#DEBUGPORT" Value=""/>
</Group>
</Block>
<DevFiles />
</Device>
<Device DevID="14212111" Model="" GroupID="">
<Block Name="DG" LastFileDld="" LastFullDld="">
<Group ZD="3">
<DSize ParamName="*UNZIP" Value="DG.ZIP"/>
<DSize ParamName="*GO" Value="F:DG.OUT"/>
</Group>
<Group ZD="15">
<DSize ParamName="#LOGPORT" Value="NONE"/>
<DSize ParamName="CDH6" Value=""/>
</Group>
</Block>
<DevFiles />
</Device>
<Device DevID="14212111" Model="" GroupID="">
<Block Name="ZG123CZ123" LastFileDld="" LastFullDld="">
<Group ZD="1">
<DSize ParamName="KEY" Value="RAM"/>
<DSize ParamName="REM" Value="***"/>
<DSize ParamName="*UNZIP" Value="I:ZG123CZ123.ZIP"/>
</Group>
<Group ZD="15">
<DSize ParamName="REM" Value="="***""/>
<DSize ParamName="#DEBUGPORT" Value=""/>
</Group>
</Block>
<DevFiles />
</Device>';
my $regex = qr/(?s)<Device(?:(?!<\/Device).)*<Block[^>]+Name="ZG123CZ123".*?<\/Device>/p;
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