Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove braces from regular expression result

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

  • http://www.postgresql.org/docs/9.1/static/functions-matching.html

Question

What is the most efficient way to remove the braces using PostgreSQL 9.x?

like image 786
Dave Jarvis Avatar asked May 15 '12 02:05

Dave Jarvis


People also ask

How do you escape braces in regular expression?

Find that brace and escape it with a backslash in front on it: \{ . You can also put it in a character class: [{] . You might have to do this in code for tools that you didn't write (I did it for my local autotools, for instance).

What do braces do in regex?

The key for this exercise is curly braces, which act as regex quantifiers — i.e., they specify the number of times the character(s) in front of the braces are found in a target search. So “{n}” tells regex to match when the preceding character, or character range, occurs n times exactly.

How do you represent brackets in regex?

[^\(]* matches everything that isn't an opening bracket at the beginning of the string, (\(. *\)) captures the required substring enclosed in brackets, and [^\)]* matches everything that isn't a closing bracket at the end of the string.


1 Answers

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:

SELECT x[1]
FROM   regexp_matches('i1 into o2, and g1 into o17', '[gio][0-9]{1,}', 'g') AS x;

Other Solutions

SELECT unnest(x)  -- also works for cases with multiple elements per result row

SELECT trim(x::text, '{}') -- corner cases with results containing `{}`

SELECT rtrim(ltrim(x::text, '{'), '}') AS x1 -- fewer corner cases

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 the SELECT list directly (like Rick provided) since behavior of multiple SRFs in the SELECT list has finally been sanitized:

  • What is the expected behaviour for multiple set-returning functions in select clause?
like image 174
Erwin Brandstetter Avatar answered Oct 13 '22 09:10

Erwin Brandstetter