开发者

what is regular expression for matching single line with multi match?

$str = "Data = [ {"name": "test","Address": "UK" "currency": "£" },{"name": "test2","Address": "US" "currency": "$" },{"name": "test","Address": "eur" "currency": "E" }

开发者_JAVA技巧I want to display all address

its not multi line string . It all a single string

Please help on this

Thanks , TREE J


Your string is JSON! Treat it as such!

edit: I'm an idiot and can't tell when a question is tagged as perl instead of PHP :-) Link ammended.


This should work:

while ($str =~ /\"Address\":\S+\"(.*?)\"/g) {
      print "Address = $1\n";
}


You do it by using the right tool for the job. In this case you fix the corrupt JSON with a regex and then use JSON to get the data:

#!/usr/bin/perl

use strict;
use warnings;

use JSON;

my $input  = <DATA>;
my ($json) = $input =~ /DATA = (.*)/;
my $data   = decode_json $json;

for my $record (@$data) {
    print "$record->{name} has address $record->{Address}\n";
}

__DATA__
DATA = [ {"name": "test",  "Address": "UK", "currency": "£" }, {"name": "test2", "Address": "US", "currency": "$" }, {"name": "test",  "Address": "eur", "currency": "E" } ]


something like:

my $str = q(Data = [ {"name": "test","Address": "UK" "currency": "£" },{"name": "test2","Address": "US" "currency": "$" },{"name": "test","Address": "eur" "currency": "E" });
my @addresses = $str =~ /"Address":\s*"([^"]*)"/g;
print "@addresses\n";

HTH,

Paul

(PS: post real code, not pseudo code...)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜