--------------------------------------------------------------------------------
\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
(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。
2条答案
按热度按时间uemypmqf1#
您可以使用此
解释
2g32fytz2#
如果不需要用与输入完全相同的空格分隔剩余的单词,那么可以将字符串转换为数组,取前四个元素,然后将其转换回字符串,其中单词用单个空格分隔:
(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
。