计算字符串中的子字符串 [英] Counting substrings in a string

查看:109
本文介绍了计算字符串中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算子字符串在字符串中出现的次数?

How can I count the numbers of times a substring shows up in a string?

在这种情况下,我每次都在前30万个互联网站点的HTML正文中出现" connect.facebook.net/en_US/all.js "(存储在httparchive中).

In this case, I'm looking for every time "connect.facebook.net/en_US/all.js" shows up in the HTML bodies of the top 300K internet sites (stored in httparchive).

推荐答案

您可以在字符串上使用SPLIT()并计算产生的记录数:

You could use SPLIT() on the string, and count the number of records produced:

SELECT fb_times, COUNT(*) n_pages
FROM
  (SELECT COUNT(splits)-1 WITHIN RECORD AS fb_times
   FROM
     (SELECT SPLIT(body, 'connect.facebook.net/en_US/all.js') splits
      FROM [httparchive:runs.2014_08_15_requests_body]
      WHERE body CONTAINS 'connect.facebook.net/en_US/all.js'
        AND mimeType="text/html"
        AND page=url))
GROUP BY 1
ORDER BY 1

请注意使用WITHIN RECORD来计数产生的SPLIT()子记录的数量.

Note the usage of WITHIN RECORD to count how many sub-records SPLIT() produced.

结果:

Fb_times    N_pages
1           12,471
2           1,222
3           163
4           34
5           18
6           12
7           12
8           6
...         ...

这篇关于计算字符串中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆