【问题标题】:ReGex for avoid emoji - Unicode/ BigQuery避免表情符号的正则表达式 - Unicode/BigQuery
【发布时间】:2018-05-13 21:21:39
【问题描述】:

我们在 Bigquery 中收到一份调查 Web 挂钩数据。本地语言的注释被捕获为 unicode 和特殊字符。我已经编写了将 unicode 转换为本地语言并执行正则表达式以避免特殊字符的函数。

#standardSQL
CREATE TEMP FUNCTION DecodeUnicode(s STRING) AS (
(SELECT CODE_POINTS_TO_STRING(ARRAY_AGG(CAST(CONCAT('0x', x) AS INT64)))
FROM UNNEST(SPLIT(s, '\\u')) AS x
WHERE x != ''
)
);
WITH NPSDashboard_Webhook_Data1_copy AS (
SELECT
TRIM(Comment) Comment
FROM
`radiant-micron-790.Sharmila_Testing.NPSDashboard_Webhook_Data1_copy`
)
,
uchars AS (
SELECT DISTINCT
c,
DecodeUnicode(c) uchar
FROM NPSDashboard_Webhook_Data1_copy,
UNNEST(REGEXP_EXTRACT_ALL(Comment, r'(\\u[abcdef0-9]{4})')) c
)

SELECT
Comment,
STRING_AGG(IFNULL(uchar, x), '' ORDER BY pos) Decoded
FROM (
SELECT
Comment,
pos,
SUBSTR(Comment,
SUM(CASE char WHEN '' THEN 1 ELSE 6 END)
  OVER(PARTITION BY Comment ORDER BY pos) - CASE char WHEN '' THEN 0 ELSE 5 
  END,
  CASE char WHEN '' THEN 1 ELSE 6 END) x,
  uchar
  FROM NPSDashboard_Webhook_Data1_copy,
  UNNEST(REGEXP_EXTRACT_ALL(Comment, r'(\\u[abcdef0-9]{4})|.')) char WITH OFFSET AS pos
  LEFT JOIN uchars u ON u.c = char
  )
  GROUP BY Comment

它的返回错误:-

  • 查询失败

  • 错误:无效的代码点 55357

我发现 "\ud83c\udf38" 它的返回错误是 "???? Cherry Blossom" 在 emoji 的返回错误。如何做正则表达式或转换器来解决这个问题?

【问题讨论】:

    标签: regex unicode emoji bigquery-standard-sql


    【解决方案1】:

    我认为你不能在纯 SQL 中做到这一点。

    我建议将 UTF-16 表情符号转换为 HTML 实体(十六进制)以将它们存储在数据库中。您很可能需要使用一种编程语言来执行此操作:

    在 .NET 中这样尝试

    using System;
    using System.Text;
    using System.Globalization;
    using System.Net;
    
    public class Program
    {
        public static void Main()
        {
    
            Console.WriteLine(WebUtility.HtmlEncode("\uD83D\uDE02"));
        }
    }
    

    或者您可以在 Java 中为此使用 emoji4j 库 (ref):

    String line = "Hi , i am fine \uD83D\uDE02 \uD83D\uDE02, how r u ?";
    EmojiUtils.hexHtmlify(line); //Hi , i am fine 😂 😂, how r u ?
    

    【讨论】:

      猜你喜欢
      • 2019-05-10
      • 2017-01-24
      • 2014-04-11
      • 2017-06-15
      • 2012-02-07
      • 1970-01-01
      • 2014-03-05
      • 2021-05-30
      • 2017-01-25
      相关资源
      最近更新 更多