regex - 跳过前导和尾随空白

标签 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 时,有没有办法删除前导和尾随空格?

最佳答案

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

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

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

enter image description here

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

关于regex - 跳过前导和尾随空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54071411/

相关文章:

python - 如何在给定约束后删除文本?

postgresql - 在 postgresql 中创建扩展时出错

sql - Postgres - 列名中的空格和特殊字符 - 如何在查询中选择这些字符

ruby-on-rails - rails 4 : ConnectionTimeoutError on heroku basic plan

regex - 正则表达式:如何接受任何符号

python - 在 python 中将所有子类组织到字典中的好方法?

regex - 使用 awk 打印正则表达式组的长度

Javascript - .split 与正则表达式返回所有匹配项(如果下一个字符不是)

python - Django 多个 unique_together 是否在 PostgreSQL 中被错误映射?

ruby-on-rails - 在 postgres/active record 中保存字符串时保持前导零