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

I am new to perl and hence this simple query.

I want to parse a string described as below to get the value '0x2f7c'.

1:05:02.599,xx:MTV = 0x2f7c:COOL_CALL, an option = 11

Please note:

a) '1:05:02.599' and '0x2f7c' are dynamic, which means to say, it keeps changing and which is not constant in the file. In other words, I can't grep these values from my code.

b) ":COOL_CALL, an option" is static, which I can use/grep from my code.

So monks can you help me as how to go about this problem? Thankz in advance

Replies are listed 'Best First'.
Re: Parsing of a string
by davido (Cardinal) on Mar 11, 2005 at 07:07 UTC

    Something like this...

    if( $string =~ m/MTV\s*=\s*(0x[a-f0-9]+)/i ) { print $1, "\n"; }

    It's just a matter of anchoring to something predictable, and capturing the right pattern. ;)

    Update: By the way, [a-f0-9] could also be represented by [[:xdigit:]] The POSIX character class for a hexidecimal digit. In that case, the RE would look like this:

    if( $string =~ m/MTV\s*=\s*(0x[[:xdigit:]]+)/ ) { print $1, "\n"; }

    It's not really gaining anything to spell it that way, but perhaps could be seen as a little clearer reading.


    Dave

Re: Parsing of a string
by ikegami (Patriarch) on Mar 11, 2005 at 07:06 UTC

    How about:

    ($num) = $string =~ /\bMTV\s*=\s*(0x[0-9a-fA-F]+)/; die("Bad Input!") unless defined $num; print("Found $num (", hex($num), " in decimal)\n");
Re: Parsing of a string
by saintmike (Vicar) on Mar 11, 2005 at 07:24 UTC
    Assuming the number you're looking for always starts with 0x and this sequence doesn't show up anywhere else in the code, use this:
    my $data = join '', <DATA>; if($data =~ /(0x\w+)/) { print "$1\n"; } __DATA__ 1:05:02.599,xx:MTV = 0x2f7c:COOL_CALL, an option = 11
    Also assuming that the hex number is terminated by a non-word ([^a-zA-Z0-9_]) character.
Re: Parsing of a string
by sh1tn (Priest) on Mar 11, 2005 at 08:49 UTC
    $_ = q{1:05:02.599,xx:MTV = 0x2f7c:COOL_CALL, an option = 11}; m/(?<==)\s?(.+)\s?(?=:)/; # $1 eq 0x2f7c


Re: Parsing of a string
by perlsen (Chaplain) on Mar 11, 2005 at 09:34 UTC

    Hi, just try this,

    my $input='1:05:02.599,xx:MTV = 0x2f7c:COOL_CALL, an option = 11'; my $match; ($match) = $input =~ m#:(?:\w+)?\s*=\s*(0x[a-f0-9]+):#i; print "$match";
Re: Parsing of a string
by rev_1318 (Chaplain) on Mar 11, 2005 at 10:10 UTC
    I'm a bit puzzed about the way everybody always comes with solutions based on assumptions. I would think, the correct response is: be more specific!

    Instead of providing an example string, with some notes, rethink your problem:
    - what is the exact pattern my strings follows?
    - what are the constant parts, which parts change?
    - what rules apply to the changing parts?

    You will notice that, ooce you gat the answers to the above questions, you have the solution to your problem

    Paul

      Tend to agree with most of your points, rev_1318, but having a hard time trying to parse their applicability here... because OP wrote ":COOL_CALL, an option" is static" -- which is pretty much an ironclad (if, 'incomplete')answer for part 2 of your question.

      Seems to me that knowing we have a (mutable) hex value followed immediately by a ":COOL_CALL" makes writing a regex perfectly straightforward... at least in the practical world where the string 'colon, COOL, uline, CALL" seems unlikely to appear accidentally.

      OTH, and to highlight my general agreement though, OP does NOT say that there will be a desired hex value immediately preceeding ":COOL_CALL" and (ymmv) I can only INFER (from the fact that its variability/non_variability is NOT addressed) that ",xx:MTV = " is potentially variable, and hence, should not be used by the regex.

      ie, greater precision in the question from OP can be a big help

Re: Parsing of a string
by Random_Walk (Prior) on Mar 11, 2005 at 11:27 UTC

    TIMTOWTDI

    >perl -le '$s="1:05:02.599,xx:MTV = 0x2f7c:COOL_CALL, an option = 11"; + (undef, $s) = unpack "a21 a6", $s; print $s' 0x2f7c >perl -le '$s="1:05:02.599,xx:MTV = 0x2f7c:COOL_CALL, an option = 11"; + $i=index $s, ":COOL_CALL"; print substr $s, $i-6, 6' 0x2f7c

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Parsing of a string
by Aaryan (Initiate) on Mar 11, 2005 at 11:24 UTC

    Fantastic guys!

    I could move forward from this issue...

    Thankz a lot 2 al the monks >:) who helped me out!!!