Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如果一个字符出现在一个单词边界内至少n次,如何替换该边界内的字符?_Regex_Postgresql - Fatal编程技术网

Regex 如果一个字符出现在一个单词边界内至少n次,如何替换该边界内的字符?

Regex 如果一个字符出现在一个单词边界内至少n次,如何替换该边界内的字符?,regex,postgresql,Regex,Postgresql,我想更新postgres表中的文本列。列中的文本仅为小写,由utf-8字符组成,如[a-z0-9\u00DF-\u00F6\u00F8-\u017E\u1E00-\u1EFF\u03B1-\u03C9]加上“(下划线)加上”(空格)。空格是分隔符。我想去掉单词边界中的下划线,只要它在该边界中出现3次以上。首选基于regexp_replace()的解决方案,但也可以使用其他特定于postgres的函数(如string_to_array unnest array_to_string等)来解决此问题

我想更新postgres表中的文本列。列中的文本仅为小写,由utf-8字符组成,如[a-z0-9\u00DF-\u00F6\u00F8-\u017E\u1E00-\u1EFF\u03B1-\u03C9]加上“(下划线)加上”(空格)。空格是分隔符。我想去掉单词边界中的下划线,只要它在该边界中出现3次以上。首选基于regexp_replace()的解决方案,但也可以使用其他特定于postgres的函数(如string_to_array unnest array_to_string等)来解决此问题

我发现了,但不能适应我的问题。还尝试了字符串到数组(),unnest(),数组到字符串(),数组(),CASE-WHEN,regex\u组合匹配,但我被卡住了

'photovoltaics renewable_energies flexible_low_cost_solar_cells colloidal_quantum_dots colloidal_quantum_dots_test1_test2'
应该成为

'photovoltaics renewable_energies flexible low cost solar cells colloidal_quantum_dots colloidal quantum dots test1 test2'
我需要一份更新声明。如何更新下表

CREATE TABLE test_table AS
SELECT 1 as id, 'photovoltaics renewable_energies flexible_low_cost_solar_cells colloidal_quantum_dots colloidal_quantum_dots_test1_test2' as word
UNION
SELECT 2,'inter_cultural_fish_studies test contact_zones knowledge_from_below'
;
我可以将@Abelisto的建议解决方案改编为以下内容:

UPDATE test_table
SET word = (select string_agg(case when length(w)-length(translate(w,'_','')) >= 3 then translate(w,'_',' ') else w end, ' ') as w from unnest(string_to_array(word, ' ')) as w)
;

不使用regexp:
选择string_agg(长度(w)-长度(平移(w,“,”)>=3时的情况),然后将(w,“,”)else w end,“)从unnest(string_到_阵列(“光伏可再生能源柔性电池低成本太阳能电池胶体量子点胶体量子点测试1_测试2’,”)转换为w上面的解决方案。由于建议的解决方案不是正则表达式,我需要一个更新语句,请参见编辑。真正的问题是有100多万行。@Abelisto:经过一番尝试,我找到了如何更新:不使用regexp:
选择string_agg(当length(w)-length(translate(w,“,”,”)>=3,然后将(w,“,”,”)else w end,“)作为w从unest(string_到_数组)('photovoltaics renewable_energies flexible_low_cost_solar_cells global_quantum_dots global_quantum_quantum_dots_test1_test2','')作为上述解决方案。