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

Hi all, I have the following string and want to get the value of start_time: {\"id\":01,\"start_time\":\"1477954800000\",\"stop_time\":\"1485817200000\",\"url\":http:://www.example.com\}

For sure one can get the value by json parsing. However out of pure curiosity I'd like to know how this can be achieved by perl regexp. I know that if the format was start_time = 1477954800000 one can get the value by the expression

:  start_time = (\w+)

But I'm quite confused how to bypass the special charachters \":\" Can anyone give a hint ?

Replies are listed 'Best First'.
Re: How to get the value after a string
by hippo (Archbishop) on Feb 08, 2017 at 15:05 UTC

    You can do this by being more general in the exclusion - you want digits following non-digits. eg:

    #!/usr/bin/env perl use strict; use warnings; use Test::More tests => 1; my $str = '{\"id\":01,\"start_time\":\"1477954800000\",\"stop_time\":\ +"1485817200000\",\"url\":http:://www.example.com\}'; my ($extracted) = $str =~ /start_time\D*(\d+)/g; is ($extracted, '1477954800000', 'Value extracted');
      That did the job. Thanks a lot.
Re: How to get the value after a string
by Laurent_R (Canon) on Feb 08, 2017 at 17:54 UTC
Re: How to get the value after a string
by Corion (Patriarch) on Feb 08, 2017 at 15:01 UTC

    Have you looked at perlre?

    What part do you have problems with exactly? Which approaches did you try and where did they fail?

Re: How to get the value after a string
by Anonymous Monk on Feb 08, 2017 at 19:36 UTC
    Not sure why the URL is not quoted, but have you ever tried something like this?
    echo '{\"id\":01,\"start_time\":\"1477954800000\",\"stop_time\":\"1485 +817200000\",\"url\":\"http:://www.example.com\"}' | perl -nle's/\\//g +;s/:(?!:)/=>/g;$s=eval$_;print $s->{start_time}'