2014-09-05 44 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

相關問題