这是我的字符串。总json响应以字符串形式出现。任务是识别子域和注释后的单词。
{item_type:a,custom_domain:“google.com”,subdomain:analytics,duration:324.33,id:2892928,comment:goahead,domain_verified:yes},{item_type:b,custom_domain:“yahoo.com”,子域名:news,comment:awesome,domain_verified:no},{item_type:c,custom_domain:“amazon.com”,subdomain:aws,width:221,image_id:3233,height:13,comment:keep it up,domain_verified: no},{item_type:d,custom_domain:“facebook.com”,subdomain:m,slug:sure,domain_verified:yes}
输出应该是,
analytics, goahead
news, awesome
aws, keep it up
m, sure
简单来说,我需要以^ subdomain开头的单词:以逗号结尾,然后以^ comment开头的单词,以逗号结尾。
传入的字符串包含大量数据。每个字符串将包含数千个子域和注释。我尝试过preg_match_all方法。但我没有得到正确的方法。
答案 0 :(得分:2)
我看到3种方式(我不确定哪种方式具有最好的性能,但我会在最后的程序方式下赌注):
with open(filename, 'r+') as resultfile:
使用程序功能,例如:
/subdomain:(.*?),.*?comment:(.*?),/
答案 1 :(得分:1)
为您提供字符串示例,您可以使用以下正则表达式来捕获所有子域:
/(subdomain:)[\w|\s]+,/gm
和
/(comment:)[\w|\s]+,/gm
捕获评论。
此处为子域名working example。
如果只想要子域或评论的内容,则可以从匹配结果中删除它们。
答案 2 :(得分:1)
试试此代码......以下是LIVE EXAMPLE
(SELECT *
FROM `my_table` t
WHERE t.ProjectId = 123 AND t.RoleId = 111
ORDER BY Title DESC
LIMIT 25
) UNION ALL
(SELECT *
FROM `my_table` t
WHERE t.ProjectId = 123 AND t.RoleId = 456
ORDER BY Title DESC
LIMIT 25
)
UNION ALL
. . . -- The other 7 combinations
ORDER BY Title DESC
LIMIT 25;
这将输出:
<?php
$string ='{item_type:a,custom_domain:"google.com",subdomain:analytics,duration:324.33, id:2892928, comment:goahead,domain_verified:yes}, {item_type:b,custom_domain:"yahoo.com",subdomain:news,comment:awesome,domain_verified:no}, {item_type:c,custom_domain:"amazon.com",subdomain:aws,width:221,image_id:3233,height:13, comment:keep it up,domain_verified:no}, {item_type:d,custom_domain:"facebook.com",subdomain:m,slug:sure,domain_verified:yes}';
$v1= explode(',',str_replace("}","",str_replace("{","",$string)));
$result =array();
foreach($v1 as $key=>$val)
{
$v2 = explode(':',$val);
if(trim($v2[0])=='subdomain' || trim($v2[0])=='comment')
{
$result[]= $v2[1];
}
}
echo implode(',',$result);
?>