2014-09-05 63 views
2

regex_matches返回字符串数组:{first match, second match}。我如何访问其中的元素?我曾尝试过:如何访问postgresql字符串数组中的元素

regex_matches('mystring', 'my string pattern')[0] 
regex_matches('mystring', 'my string pattern') as url[0] 
regex_matches('mystring', 'my string pattern') as url, url[0] 

没有用。我真的需要做一个字符串函数来替换两个大括号吗?这似乎很笨重

回答

3

,你必须使用额外的括号:

postgres=# select regexp_matches('123 333'::text, '\d{3}'::text, 'g'); 
regexp_matches 
---------------- 
{123} 
{333} 
(2 rows) 

postgres=# select (regexp_matches('123 333'::text, '\d{3}'::text, 'g'))[1]; 
regexp_matches 
---------------- 
123 
333 
(2 rows) 
+0

就像一个魅力。非常感谢! – 1252748 2014-09-05 21:38:51