开发者

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 loo开发者_StackOverflowking 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; }


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.


Perl JSON.


You really shouldn't use regular expressions anywhere you need to find something in a string... They have their purpose, but not here.

If you have a JSON encoded string just decode it. I have no experience with Perl but I see that others recommend using a module from CPAN.


First you need a regex that matches the text you want to capture. Since you only put a snippet I will only put a snippet of it also.

$text = ' ..E","location":"Hollywood, Los Angeles, CA ","screen_name":"i..';
if( $text =~ /.*location":"(.[^"]+)",.*/ ) {
  $tmp_loc = $1;
}
return $tmp_loc;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜