我需要从网站上获取不同类型的信息。知道格式是这样的(都在一行上):
"name":"BLAHBLAH","contact":{"phone":"12345","twitter":"BLAHBLAH"},
"location": "address":"NOTTELLING","lat":10,"lng":10,"postalCode":"1234",
"city":"BLAH","state":"BLAH","country":"BLAH"},
"categories":[{"id":"BLAH","name":"BLAH"}]
其中一些配置文件会缺少一两个块,例如没有名称,没有城市等。我尝试了这样的代码:
#get name
$content =~ m!","name":"(.*?)","contact":!igs;
say ("name:", $1) unless ($1 eq '');
#get street
if ($content =~ m!\},"location":\{"address":"(.*?)","lat":!igs)
{say ("street:", $1) unless ($1 eq '');}
#get city
if ($content =~ m!,"city":"(.*?)","state":!igs)
{say ("city:", $1) unless ($1 eq '');}
#get state
if ($content =~ m!,"state":"(.*?)","country":!igs)
{say ("state:", $1) unless ($1 eq '');}
然后我意识到当我使用重复模式时(例如
"city":"(.*?)","state":!igs
和
"state":"(.*?)","country":!igs)
结果将不准确。(我猜 perl 在第一次到达“state”后会跳过字符串)我的问题是这是怎么发生的?我该如何解决?