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

I would like to extract the value for 'var nonce' from the following text blob (string)

"... var realm = "heaven"; var nonce = "186248:541534:a67930c5cffeb25a5485e6b4f32570e2"; var qop = "auth"; var uri = "/earth.lp"; ...."

Currently my code looks a like:

my $req = HTTP::Request->new(GET => $server_endpoint); my $resp = $ua->request($req); my $message = $resp->decoded_content; $message =~ m/nonce/i; print " starts: @- \n ends: @+ \n";

To get the begin and end of the nonce keyword (and next I will need to extract the value (186248:541534:a67930c5cffeb25a5485e6b4f32570e2) ).

Problem is I do not get the start or end position back on this string? what am I missing here?

Replies are listed 'Best First'.
Re: extract pattern from large blob of text
by Corion (Patriarch) on Mar 06, 2015 at 14:40 UTC

    @- and @+ are arrays, as documented in perlvar. Maybe you wanted to actually capture things?

    My approach would be to also capture the nonce value in the same regular expression match:

    if( $message =~ /nonce\s+=\s+"([^"]+)"/ ){ my $nonce= $1; print "Found nonce: $nonce\n"; } else { die "No nonce found in message:" . $message; };

      very usable example, thx

      Seems the formatting of $message is not correct to me, I have added how I get to the content of $message to the original post.

        If you think the formatting of $message is not what you want it to be, what steps have you taken to confirm this?

        Maybe printing $message might show you the content?

Re: extract pattern from large blob of text
by hdb (Monsignor) on Mar 06, 2015 at 14:52 UTC

    ...and why are you not directly extracting the string, like /var nonce = "(.*?)";/? (untested) $1 will contain what you look for.

      thank you very much for the great assistance, case solved.