替换包含第一个列表(模式)中某些元素的文本,然后替换为Clickhouse中其他列表(替换)中的元素

发布于 2025-02-05 18:24:35 字数 1702 浏览 2 评论 0原文

你好,我有这个表

WITH datos as (WITH ['This not change','this text is the second word','this is a exact mach','and this not change'] as datosSup
select arrayJoin(datosSup) as Title)
select * from datos

我的表

标题
第一个单词
这个文字是第二个单词
,这是一个确切的马赫
,这不是更改

和两个列表,

['is the second word','this is a exact mach'] as patterns,
['contains second word','replaced text exact match'] as replacements

我能够替换文本,如果“ title title < /em>“字段精确匹配列表模式的一个元素

WITH datos as (WITH ['first word','this text is the second word','this is a exact mach','and this not change'] as datosSup
select arrayJoin(datosSup) as Title),
['is the second word','this is a exact mach'] as pattern,
['contains second word','replaced text exact match'] as replacement
select if(indexOf(pattern,Title)>0,replacement[indexOf(pattern,Title)],Title)  from  datos

结果:

标题
第一个单词
此文本是第二个单词
替换文本精确匹配
,而这不是更改

,但我想替换文本,包含 /strong>第一个列表中的某些元素(模式),并替换为其他列表中的元素(替换)。

所需结果:

标题
第一字
此文本包含第二个字
替换文本精确匹配
,而这不会改变

Hello I have this table

WITH datos as (WITH ['This not change','this text is the second word','this is a exact mach','and this not change'] as datosSup
select arrayJoin(datosSup) as Title)
select * from datos

My Table

Title
first word
this text is the second word
this is a exact mach
and this not change

And two lists,

['is the second word','this is a exact mach'] as patterns,
['contains second word','replaced text exact match'] as replacements

I have been able to replace text if "Title" field exact match with one element of list patterns

WITH datos as (WITH ['first word','this text is the second word','this is a exact mach','and this not change'] as datosSup
select arrayJoin(datosSup) as Title),
['is the second word','this is a exact mach'] as pattern,
['contains second word','replaced text exact match'] as replacement
select if(indexOf(pattern,Title)>0,replacement[indexOf(pattern,Title)],Title)  from  datos

Result:

Title
first word
this text is the second word
replaced text exact match
and this not change

But I want replace text which contains some element from first list (patterns) and replace it by elements in other list (replacements).

Result desired:

Title
first word
this text contains second word
replaced text exact match
and this not change

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2025-02-12 18:24:35

以防万一它可以帮助任何人,我在另一个论坛中回答,如下所示,

SELECT arrayJoin(if(length(replaced) = 0, [Title], replaced)) AS replaced_title
FROM
(
    SELECT
        Title,
        groupArray(my_replace(Title, pattern, replacement)) AS replaced
    FROM t, p
    GROUP BY Title
)

表P为(模式字符串,替换字符串)。不幸的是,Clickhouse的替换()仅用于模式和更换。因此,我制作了一个函数my_replace

CREATE OR REPLACE FUNCTION my_replace AS
    (haystack,pattern,replacement) -> 
    if(position(haystack,pattern)== 0, NULL,substring(haystack, 1, position(haystack, pattern)-1) ||
                 replacement || 
                 substring(haystack, position(haystack, pattern) + length(pattern),
                 length(haystack) - position(haystack, pattern) - length(pattern) + 1))

结果

标题
第一个单词
替换了文本精确匹配
,而这不更改
此文本包含第二个字

Just in case it helps anyone, I was answered in another forum as follows

SELECT arrayJoin(if(length(replaced) = 0, [Title], replaced)) AS replaced_title
FROM
(
    SELECT
        Title,
        groupArray(my_replace(Title, pattern, replacement)) AS replaced
    FROM t, p
    GROUP BY Title
)

Table p is (pattern String, replacement String). Unfortunately ClickHouse's REPLACE() takes constant only for pattern and replacement. So I made a function my_replace

CREATE OR REPLACE FUNCTION my_replace AS
    (haystack,pattern,replacement) -> 
    if(position(haystack,pattern)== 0, NULL,substring(haystack, 1, position(haystack, pattern)-1) ||
                 replacement || 
                 substring(haystack, position(haystack, pattern) + length(pattern),
                 length(haystack) - position(haystack, pattern) - length(pattern) + 1))

Result

Title
first word
replaced text exact match
and this not change
this text contains second word
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文