跳过前导和尾随空白

时间:2019-01-07 09:14:21

标签: regex postgresql

我正在使用regexp_split_to_array通过如下切割空白来分割文本:

select regexp_split_to_array('  this   is    just    me   ', '\s+');

这给出了:

{"",this,is,just,me,""}

我想要拥有:

{this,is,just,me}

应用regexp_split_to_array时是否可以删除开头和结尾的空格?

3 个答案:

答案 0 :(得分:1)

我建议采用相反的方法:用\S+正则表达式模式匹配除空格以外的所有字符:

select regexp_matches('  this   is    just    me   ', '\S+', 'g')

请参见online demo。请注意,'g'参数将启用多个匹配。

enter image description here

如有必要,在正则表达式匹配项上使用unnest将数组扩展为一组行。

答案 1 :(得分:0)

这应该做到:

SELECT REGEXP_SPLIT_TO_ARRAY(TRIM('  this   is    just    me   '), '\s+');

TRIM(不带参数)将从字符串中remove both leading and trailing whitespace

答案 2 :(得分:0)

在将字符串传递给trim函数之前,尝试在字符串上使用regex_split_to_array

select regexp_split_to_array(trim('  this   is    just    me   '), '\s+');
相关问题