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

I have a string:
P7: "P7": place in [from Mem:0x5ffa5800 to Mem:0x60077fff] {
I need to extract both Hex Values and subtract value 2 from value 2 There are allot of ways to parse a astring and wanted to be a little more elegant in the approach. I am not getting an matches at all with this and it should work.
if ($line =~ m/"P7"\:/) { if ( $line =~ /\(.*Mem:\)\(0x[0-9a-fA-F]*\)\( to Mem:\)\(0x[0-9a-fA- +F]*\) ) { print "First Value $2 \n"; print "Second Value $4 \n "; } } }

Replies are listed 'Best First'.
Re: Parse Hex Values
by toolic (Bishop) on Sep 23, 2013 at 18:28 UTC

    Here are some changes I'd make:

    • Don't escape the parens
    • Only capture the things you are using
    • Use the xdigit character class
    • Use + instead of *
    use warnings; use strict; while (my $line = <DATA>) { if ($line =~ m/"P7"\:/) { if ( $line =~ /Mem:(0x[[:xdigit:]]+) to Mem:(0x[[:xdigit:]]+)/) +{ print "First Value $1 \n"; print "Second Value $2 \n "; } } } __DATA__ P7: "P7": place in [from Mem:0x5ffa5800 to Mem:0x60077fff]

      Hi Apologies for the lack of tags. The regex supplied worked perfectly and I want to thank you

Re: Parse Hex Values
by AnomalousMonk (Archbishop) on Sep 23, 2013 at 21:46 UTC

    Another way, although close to toolic's and guessing a bit about the actual string (\K available with Perl 5.10+):

    >perl -wMstrict -le "use 5.010; ;; my $P7 = '\"P7\": place in [from Mem:0x5ffa5800 to Mem:0x60077fff] {' +; print qq{'$P7'}; ;; if ($P7 =~ m{ \"P7\" }xms) { my ($h1st, $h2nd) = $P7 =~ m{ 0[Xx] \K [[:xdigit:]]+ }xmsg; print qq{first value '$h1st' second value '$h2nd'}; } else { print q{no '\"P7\"'}; } " '"P7": place in [from Mem:0x5ffa5800 to Mem:0x60077fff] {' first value '5ffa5800' second value '60077fff'

    And yes, please, please use code tags, proper readable formatting in future.

Re: Parse Hex Values
by marinersk (Priest) on Sep 23, 2013 at 18:19 UTC
    Please see Writeup Formatting Tips -- your post will be easier to read if you use <p> and <br> to format your text, and use <code> and </code> tags to format your code, data, and results.

    Update:Here's the best I could get out of your code as written; I am sure I mistranslated something in the effort to compensate for the lack of <code> and </code> tags.

    #!/usr/bin/perl -w use strict; # ---------------[ Original request ]--------------- # I have a string: P7: "P7": place in [from Mem:0x5ffa5800 to Mem:0x60 +077fff] # { I need to extract both Hex Values and subtract value 2 from value +2 # There are allot of ways to parse a astring and wanted to be a little + more elegant in the approach. # I am not getting an matches at all with this and it should work. my $line = "P7: \"P7\": place in [from Mem:0x5ffa5800 to Mem:0x6007 +7fff]"; print "\$line = '$line'\n"; if ($line =~ m/"P7"\:/) { print "\$line matches P7\n"; if ( $line =~ /\(.*Mem:\)\(0x[0-9a-fA-F]*\)\( to Mem:\)\(0x[0-9a-f +A-F]*\) ) { print "First Value $2 \n"; print "Second Value $4 \n "; } else { print "Did not match second test\n"; } } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-09-23@1223-HexCalc>HexCalc.pl Search pattern not terminated at C:\Steve\Dev\PerlMonks\P-2013-09-23@1 +223-HexCalc\HexCalc.pl line 18.