我一直在玩psql并将名称拆分成一个数组,例如:
select string_to_array('joseph jones', ' ');
string_to_array
-----------------
{joseph,jones}
这完全符合我的预期。
但是,我的数据集包含很多姓氏以“ o”开头的姓氏。
select string_to_array('joseph o carroll', ' ');
string_to_array
-----------------
{joseph,o,carroll}
有什么办法可以添加一些额外的逻辑,以便在单词前面加一个'o'时将其捆绑到下一个单词中?
所以约瑟夫·卡洛尔会回来 {joseph,o carroll}
你不能仅仅使用一个(?<!o)\s+
,尝试一下romeo bones
。由于名字的结尾是o
,所以表达式不匹配。
采用
select regexp_split_to_array('joseph o jones','(?<!\yo)\s+');
说明
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\y the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
o 'o'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句