Postgres中不能小写反向引用的字符串

时间:2015-11-20 09:04:40

标签: sql regex postgresql string-function

我正在尝试小写一个反向引用的字符串,但它不会工作。 :(我只想降低root之后的第一个字符。

my_string = "<root>Here is July's best food</root>"

正确的应该是:

my_string = "<root>here is July's best food</root>"

这是我的问题:

UPDATE my_table
SET my_string = regexp_replace(my_string, '<root>(.)', lower(E'<root>\\1'))
WHERE my_id = 1;

1 个答案:

答案 0 :(得分:0)

使用split_part()lower()

 select '<root>'||lower(split_part(split_part('<root>Here is July''s best food</root>',' ',1),'>',2)) || split_part('<root>Here is July''s best food</root>',split_part(split_part('<root>Here is July''s best food</root>',' ',1),'>',2),2)

结果:

 "<root>here is July's best food</root>"
UPDATE my_table
SET my_string =  '<root>'||lower(split_part(split_part(my_string,' ',1),'>',2)) || split_part(my_string,split_part(split_part(my_string,' ',1),'>',2),2) 
WHERE my_id = 1;

以下功能是可选的

您可以制作如下所示的功能

 create or replace function lower_first_word(txt text,del text) returns text as
$$
 select del||lower(split_part(split_part(txt ,' ',1),'>',2)) || split_part(txt ,split_part(split_part(txt ,' ',1),'>',2),2)
$$
language sql 

并更新

UPDATE my_table
    SET my_string = lower_first_word('<root>Here is July''s best food</root>','<root>')
    WHERE my_id = 1;

<强> OR

使用regexsubstring()

select lower(substring('<root>Here is July''s best food</root>',E'[A-Za-z<>]+'))||substring('<root>Here is July''s best food</root>',E'[^ ]* (.*)')

并且更新语句是

UPDATE my_table
SET my_string = lower(substring('<root>Here is July''s best food</root>', E '[A-Za-z<>]+')) || substring('<root>Here is July''s best food</root>', E '[^ ]* (.*)')
WHERE my_id = 1;