mark4444az has asked for the wisdom of the Perl Monks concerning the following question:

I have a case where I have to pick up 2 values from a line: 3p3V_MON Trip = 2.713V/2.681V I can get the first one ok by using this ($volts) = ($line =~ /3p3V_MON Trip\s+=\/([\d\.]+)/); But I also have to get the second value, in this case 2.681. To further complicate things I'm not guaranteed that the first value will always be the same number of digits. Anyone have any ideas?

Replies are listed 'Best First'.
Re: regular expression to get a decimal value
by toolic (Bishop) on May 17, 2011 at 14:34 UTC
    use warnings; use strict; my $line = '3p3V_MON Trip = 2.713V/2.681V'; if ($line =~ m{ 3p3V_MON \s+ Trip \s+ = \s+ ( [\d.]+ ) V/ ( [\d.]+ ) } +x) { print "$1 $2\n"; } __END__ 2.713 2.681
      That works great!! But... How does the result get into the $1 and $2 strings? (novice here, just want to know how it works)
Re: regular expression to get a decimal value
by wind (Priest) on May 17, 2011 at 15:01 UTC
    use strict; use warnings; my $str = '3p3V_MON Trip = 2.713V/2.681V'; if ($str =~ m{([\d.]+)V/([\d.])}) { print "$1 $2\n"; } else { warn "Invalid data format: $str"; }
Re: regular expression to get a decimal value
by SuicideJunkie (Vicar) on May 17, 2011 at 15:03 UTC

    I don't know about the other portions of the logging, but to me that looks like:

    • split on the "=" and look at the right hand side.
    • Look for: set of digits, optional decimal point, optional more digits, ending with a "V"

    I'd probably use ([0-9]+\.?[0-9]*)V with a /g to grab them all (both).

      For a decimal value, I'd use /[0-9]+(?:\.[0-9]*)?|\.[0-9]+/