【问题标题】:REGEXP_REPLACE for spark.sql()spark.sql() 的 REGEXP_REPLACE
【发布时间】:2021-06-07 06:49:45
【问题描述】:

我需要为 spark.sql() 作业编写 REGEXP_REPLACE 查询。 如果值遵循以下模式,则仅提取第一个连字符之前的单词并将其分配给目标列“名称”,但如果模式不匹配,则应报告整个“名称”。

图案:

  1. 值应该用连字符分隔。任何值都可以出现在第一个连字符之前(无论是数字, 字母、特殊字符甚至空格)
  2. 第一个连字符后面应该紧跟两个单词,用连字符隔开(只能是数字, 字母或字母数字)(注意:不允许使用特殊字符和空格)
  3. 两个词后应接一个或多个数字,后接连字符。
  4. 最后一部分只能是一位或多位数字。

例如:

如果 name = abc45-dsg5-gfdvh6-9890-7685,则输出 REGEXP_REPLACE = abc45

如果 name = abc,则输出 REGEXP_REPLACE = abc

如果 name = abc-gf5-dfg5-asd5-98-00,则 REGEXP_REPLACE 的输出 = abc-gf5-dfg5-asd5-98-00

我有

spark.sql("SELECT REGEXP_REPLACE(name , '-[^-]+-\\w{2}-\\d+-\\d+$','',1,1,'i')  AS name").show();

但它不起作用。

【问题讨论】:

标签: sql regex apache-spark apache-spark-sql regexp-replace


【解决方案1】:

使用

^([^-]*)(-[a-zA-Z0-9]+){2}-[0-9]+-[0-9]+$

proof。替换为$1。如果$1 不起作用,请使用\1。如果\1 不起作用,请使用\\1

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^-]*                    any character except: '-' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (                        group and capture to \2 (2 times):
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
    [a-zA-Z0-9]+             any character of: 'a' to 'z', 'A' to
                             'Z', '0' to '9' (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  ){2}                     end of \2 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \2)
--------------------------------------------------------------------------------
  -                        '-'
--------------------------------------------------------------------------------
  [0-9]+                   any character of: '0' to '9' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  -                        '-'
--------------------------------------------------------------------------------
  [0-9]+                   any character of: '0' to '9' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

  • 这似乎是一个更好的解决方案 +1
猜你喜欢
  • 2021-06-20
  • 2019-01-19
  • 1970-01-01
  • 2020-01-03
  • 2018-07-02
  • 2022-06-27
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多