Problem
This code:
select
x::text
from
regexp_matches( 'i1 into o2, and g1 into o17', '[gio][0-9]{1,}', 'g' ) as x;
Returns these results:
{i1}
{o2}
{g1}
{o17}
Rather than the following results:
i1
o2
g1
o17
Related Links
Question
What is the most efficient way to remove the braces using PostgreSQL 9.x?
Optimal Solution
Your
regexp_matches()
pattern can only result in a single element per pattern evaluation, so all resulting rows are constrained to exactly one array element. The expression simplifies to:Other Solutions
If the pattern can or shall not match more than one time per input value, also drop the optional parameter
'g'
.And if the function shall always return exactly one row, consider the subtly different variant
regexp_match()
introduced with Postgres 10.In Postgres 10 or later it's also prudent to suggest the set-returning function (SRF)
regexp_matches()
in theSELECT
list directly (like Rick provided) since behavior of multiple SRFs in theSELECT
list has finally been sanitized: