[英]REGEXP_REPLACE for spark.sql()
我需要为 spark.sql() 作业编写 REGEXP_REPLACE 查询。 如果值遵循以下模式,则仅提取第一个连字符之前的单词并将其分配给目标列“名称”,但如果模式不匹配,则应报告整个“名称”。
图案:
例如:
如果名称 = abc45-dsg5-gfdvh6-9890-7685 , output 的REGEXP_REPLACE = abc45
如果名称 = abc , output 的REGEXP_REPLACE = abc
如果名称 = abc-gf5-dfg5-asd5-98-00 , output 的REGEXP_REPLACE = abc-gf5-dfg5-asd5-98-00
我有
spark.sql("SELECT REGEXP_REPLACE(name , '-[^-]+-\\w{2}-\\d+-\\d+$','',1,1,'i') AS name").show();
但它不起作用。
利用
^([^-]*)(-[a-zA-Z0-9]+){2}-[0-9]+-[0-9]+$
见证明。 替换为$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.