如何在re2中找到一个正则表达式来搜索字符串?

时间:2016-11-14 03:53:11

标签: regex google-sheets formulas regex-greedy

我需要在Excel的RE2语法中提取如下所示的查询。问题是我必须掌握所有内容(包括标点符号和单词数量)" name":" "},

注意:这只是一个单元格,我的电子表格中有很多这样的单元格

interests":[
{"id":"1","name":"West Bromwich Albion F.C."}, 
{"id":"2","name":"Tottenham Hotspur F.C."},
{"id":"3","name":"Arsenal F.C."},
{"id":"4","name":"Liverpool F.C."},
{"id":"5","name":"Sunderland A.F.C."},
{"id":"6","name":"EPL"},    
{"id":"7","name":"Southampton F.C."},
{"id":"8","name":"Manchester United F.C."},
{"id":"9","name":"A-League"},
{"id":"10","name":"West Ham United F.C."},
{"id":"11","name":"Chelsea F.C."},
{"id":"12","name":"Burnley Football Club"},
{"id":"13","name":"Barclays English Premiership"},
{"id":"14","name":"Stoke City F.C."},
{"id":"15","name":"Arsenal"},
{"id":"16","name":"Crystal Palace F.C."},
{"id":"17","name":"Football"},
{"id":"18","name":"Aston Villa F.C."},
{"id":"19","name":"Norwich City F.C."},
{"id":"20","name":"Leicester City F.C."},
{"id":"21","name":"Everton F.C."},
{"id":"22","name":"Arsenal Fans"},
{"id":"23","name":"Manchester City F.C."}]}],

我的公式是

=iferror(REGEXEXTRACT(K98, "name\W:\W(.*?)\W}"), "")

但它只返回列中的一个我需要同一列中的足球俱乐部的所有名称或单独的。

我也尝试过使用

=regexreplace(K98,"^interests\W+id\W\W+\w+\W+name\W:\W(.*?)\W},\W+id\W\W+\w+\W+name\W:\W(.*?)\W},\W+id\W\W+\w+\W+name\W:\W(.*?)\W}]}],$","$1, $2, $3")

放入同一列但返回以下(示例)

interests":[{"id":"1","name":"Movies"},{"id":"2","name":"Entertainment"}]}],

b)当它无法找到时,所以我为每个案例设置了一个regexmatch但是(。?)仍然会读取案例< - 我如何处理(。?)

1 个答案:

答案 0 :(得分:1)

您可以创建3个捕获组,然后使用regexreplace,将字符串替换为第二个捕获组,如下所示:

=REGEXREPLACE(A2,"({""id"":""\d+"",""name"":"")(.*)(""},)","$2")

enter image description here

如果您想使用arrayformula,只需将引用更改为A2:A,如下所示:

=ARRAYFORMULA(REGEXREPLACE(A2:A,"({""id"":""\d+"",""name"":"")(.*)(""},)","$2"))

enter image description here

要将它们全部放在一个单元格中,您可以使用连接包装函数,例如:

=join(";",ARRAYFORMULA(REGEXREPLACE(A2:A,"({""id"":""\d+"",""name"":"")(.*)(""},)","$2")))

我用的是“;”作为我的分隔符 - 您可以有效地使用您选择的任何分隔符