八、clickhouse字符串替換函數


-- 替換匹配到的字符串
-- replaceOne(haystack, pattern, replacement)

-- 用‘replacement’子串替換‘haystack’中與‘pattern’子串第一個匹配的匹配項(如果存在)。 ‘pattern’和‘replacement’必須是常量。
-- replaceAll(haystack, pattern, replacement), replace(haystack, pattern, replacement)
-- 用‘replacement’子串替換‘haystack’中出現的所有‘pattern’子串。
SELECT
replaceOne('hed1234544', '4', '*') AS replaceOne,-- hed123*544
replaceRegexpOne('hed1234544', '4', '*') AS replaceRegexpOne,-- hed123*544
replace('hed1234544', '4', '*') AS replace, -- hed123*5**
replaceAll('hed1234544', '4', '*') AS replaceAll;-- hed123*5**

-- 實例:2019-07-31 改變成 07/31/2019
SELECT
toDate(now()) AS now_date,
replaceRegexpOne(toString(now_date), '(\\d{4})-(\\d{2})-(\\d{2})', '\\2/\\3/\\1') AS format_date;
-- 示例:賦值字符串10次
SELECT replaceRegexpOne('Hello, World!', '.*', '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0') AS res;
-- replaceRegexpAll(haystack, pattern, replacement)
-- 與replaceRegexpOne相同,但會替換所有出現的匹配項。例如:
SELECT replaceRegexpAll('hello,world!', '.', '\\0\\0') as res; -- hheelllloo,,wwoorrlldd!!
SELECT replaceRegexpAll('hello o o, world.', ' ', '*') as res; -- hello*o*o,*world.

 

-- 函數:regexpQuoteMeta(s) 該函數用於在字符串中的某些預定義字符之前添加反斜杠。
-- 預定義字符:'0','\','|','(',')','^','$','。','[',']','?','* ','+','{',':',' - '。
-- 這個實現與re2 :: RE2 :: QuoteMeta略有不同。它以\0而不是\x00轉義零字節,它只轉義所需的字符
---- 簡言之,就是不處理轉義字符,一般如果沒有用的這個函數,都會有轉義的情況出現。

SELECT regexpQuoteMeta('\\\\|[]{}+_-=@!~`&^*%$#'); -- \\\\\|\[\]\{}\+_\-=@!~`&\^\*%\$#
SELECT toString('\\\\'); -- \\

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM