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

In my code below, I am trying to extract scalar string data from HTML using a regular expression.  I should end up with $message eq '"This is the message."' (please note that the double-quotes should be included in the string) and $sender eq "sender".  For some reason that I can't figure out, I always get an empty string in each scalar variable.  Please point out the error of my ways....

$data = qq/{"message":"This is the message.","sendAs":"sender"}/; ( $message, $sender ) = $data =~ /\{"message":("[^"]"),"sendAs":"([^"] +)"\}/;

Replies are listed 'Best First'.
Re: Regular expression not working
by ikegami (Patriarch) on Jan 04, 2010 at 17:26 UTC
    "[^"]" should be "[^"]*". I make that error all the time. But why aren't you using a JSON or YAML module to parse JSON? (JSON is a subset of YAML.)

    I always get an empty string in each scalar variable.

    No, you get undef in each of them.

      Oh man, do I feel stupid!  Adding the asterisk makes everything work as desired.  I guess that's one of the few times that Perl will do what you tell it to, instead of what you want it to.  Thank you for helping ease my shame with your kind remark.

      Is it worth the added complexity to use a JSON module, if all I want are the two strings listed here?

        Is it worth the added complexity to use a JSON module, if all I want are the two strings listed here?

        You have it backwards. Calling a function is much simpler than developing a long regex pattern. As it is, your parser is highly incomplete. It can't even handle simple things like strings with quotes in them.

        Is it worth the added complexity to use a JSON module, if all I want are the two strings listed here?
        Considering that you had to go to perlmonks and ask before you had a correct solution, I believe the answer is yes.

      By the way, if you're feeling incredibly altruistic today, I would appreciate any input you might offer on another thread of mine, dealing with JSON that's been chunked and gzip'ed....

        Corion has already posted what I'd do. Recreate the stream (somehow), create an HTTP::Response object from it (->parse), and let HTTP::Response remove the transfer encoding (->decoded_content).