Search code examples
regexperltwittertwitter-feed

How do I substract a substring from a JSON string using a regular expression?


So I found these links which are related for the task. First using python, second using c#, third using Perl

Now I'm too new with Perl and what I want to do is work with some json streams from twitter. What I'm looking at is this:

..E","location":"Hollywood, Los Angeles, CA ","screen_name":"i..

How do I find "location": using regex and then assign a variable to contain Hollywood, Los Angeles, CA?

sub get_location {
# pseudo code:  
# look for "location":"xxxxxxxxxxxxxxxx" 
# assign $tmp_loc = Hollywood, Los Angeles, CA (in this case)
# return $tmp_loc; }

Solution

  • Perl has libraries for dealing with JSON. Why not use one of those?

    Alternatively, as you're dealing with Twitter, why not use Net::Twitter which makes Twitter API calls and returns the results to you as Perl data structures.

    These days, a lot of Perl programming is a matter of knowing which CPAN modules to string together. If you're not using CPAN, then you're missing out on a lot of the power of Perl.