use strict;
my $str = '<Measurements>
<Measurement Value="237.6" Unit="V" Type="AC_Voltage"/>
<Measurement Value="9.828" Unit="A" Type="AC_Current"/>
<Measurement Value="2344.7" Unit="W" Type="AC_Power"/>
<Measurement Value="2343.4" Unit="W" Type="AC_Power_fast"/>
<Measurement Value="50.003" Unit="Hz" Type="AC_Frequency"/>
<Measurement Value="294.4" Unit="V" Type="DC_Voltage1"/>
<Measurement Value="171.3" Unit="V" Type="DC_Voltage2"/>
<Measurement Value="8.056" Unit="A" Type="DC_Current1"/>
<Measurement Unit="A" Type="DC_Current2"/>
<Measurement Value="343.8" Unit="V" Type="LINK_Voltage"/>
<Measurement Unit="W" Type="GridPower"/>
<Measurement Unit="W" Type="GridConsumedPower"/>
<Measurement Unit="W" Type="GridInjectedPower"/>
<Measurement Unit="W" Type="OwnConsumedPower"/>
<Measurement Value="100.0" Unit="%" Type="Derating"/>
</Measurements>
</Device>
</root>';
my $regex = qr/(\d.+)(?=\s*\" Unit=\"W\" Type=\"AC_Power\")/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