我有一个很长的字符串,我试图从中捕获2个值。 我原来的字符串:
$string = 'content' => '{"sender":"xxx.xxx@icloud.com","location":"Beacon","event":"LocationTest","time":"2014-11-20 03:24:19 +0000"}',
'auto_upgrade' => 1,
'max_memory_size' => 262144
我想拥有位置" Beacon"在名为$location
的var和事件" LocationTest"在名为$event
的var中
标题" location":和" event":将始终出现在我的字符串中。用正则表达式做任何简单的方法吗?
答案 0 :(得分:0)
您可以使用
形式的正则表达式位置:
/(?<="location":)"[^"]*"/
for event
/(?<="event":)"[^"]*"/
示例代码:
$str='"sender":"xxx.xxx@icloud.com","location":"Beacon","event":"LocationTest","time":"2014-11-20
03:24:19 +0000"';
$str =~ m/(?<="location":)("[^"]*")/;
print 'Location ',$1;
$str =~ m/(?<="event":)("[^"]*")/;
print 'event ',$1;
会产生输出
Location "Beacon"
event "LocationTest"
答案 1 :(得分:0)
答案 2 :(得分:0)
这看起来像JSON,这意味着通过使用JSON::XS等模块,从长远来看,你会好得多。