Postgresql中的REGEXP_LIKE

我有一个表来存储替换,该表包括两个字段,第一个用于存储单词,第二个用于存储替换。我知道创建表不是一种合适的方法,但是它已经存在并且已被其他系统使用。

该表如下所示:

WORD        SUBS_LIST
------------------------------------
MOUNTAIN    MOUNTAIN,MOUNT,MT,MTN
VIEW        VIEW,VU
FIFTH       V,5TH
YOU         EWE,U,YEW
ROW         ROW,ROE
ONE         UN,ONE

然后,当输入名称时,将根据表替换该名称。我能够使用regexp_like在Oracle上进行操作。但是,我想在Postgresql中应用相同的内容。我尝试使用〜替换regexp_like和regexp_matches失败。

请找到here到目前为止我尝试过的DB Fiddle。

谢谢您的帮助:)

Sunny_Frankk 回答:Postgresql中的REGEXP_LIKE

您不需要正则表达式。如果我正确理解了您想输入的单词,请搜索sub_list中的元素,然后为此返回word列。最好的方法是将(丑陋的)逗号分隔列表转换为数组,然后使用ANY运算符:

select word
from the_table
where 'mount' = any(string_to_array(subs_list,','));

上面的方法可以正确处理,周围的空白-不确定这是否是格式设置的结果,还是真的以这种方式存储列表。如果确实需要处理空格,则可以使用以下命令:

select word
from the_table
where exists (select *
              from unnest(string_to_array(subs_list,')) as x(subs)  
              where trim(x.subs) = 'mount');

如果您的输入是单词列表,则可以使用regexp_split_to_table()将输入的单词转换为行并加入替代词。

SELECT w.input,coalesce(x.word,w.input) as word
FROM regexp_split_to_table('MOUNT VU FOOD CAFE','\s') as w(input) 
  LEFT JOIN (
    select s.word,trim(s1.token) as token
    from subs s
      cross join unnest(string_to_array(s.subs_list,')) s1(token)
  ) as x on lower(trim(w.input)) = lower(x.token)
;

在线示例:https://rextester.com/DZBF77100

本文链接:https://www.f2er.com/3017602.html

大家都在问