postgresql 如何保留课文的前4个字?

gajydyqb  于 2022-12-29  发布在  PostgreSQL
关注(0)|答案(2)|浏览(106)

我想缩短第四个字的字段:

unaccent('unaccent', lower(regexp_replace(titre, '[^\w]+','_','g')))
uemypmqf

uemypmqf1#

您可以使用此

=regexextract(A2,"\w+(?:\W+\w+){5}")

解释

--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (21 times):
--------------------------------------------------------------------------------
    \W+                      non-word characters (all but a-z, A-Z, 0-
                             9, _) (1 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  ){5}                    end of grouping
2g32fytz

2g32fytz2#

如果不需要用与输入完全相同的空格分隔剩余的单词,那么可以将字符串转换为数组,取前四个元素,然后将其转换回字符串,其中单词用单个空格分隔:

array_to_string((regexp_split_to_array(titre, '[^\w]'))[1:4], ' ')

(regexp_split_to_array(titre, '[^\w]')会将字符串one two three four five six转换成一个有6个元素的数组,然后[1:4]提取前4个元素(如果少于4个元素,则提取所有元素),array_to_string将其转换回字符串,因此one two three four five six会被转换成one two three four
但是,one two three four five six也将转换为one two three four

相关问题