I want get characters between [] from a string
example:
"hello" and "world"
from
"gegerferf[hello] frfer [world] frfre rfrf"
into a table
i am try with substring and for loop but i dont now.
select regexp_replace(string, '...','', 'g')
from mytable
2
Answers
The question didn’t provide an example of the expected results, so I’ll present a couple of options.
The following is run to setup the demonstration context:
This query returns the text between brackets concatenated into a single string:
The result is:
This query returns the text between brackets as separate rows:
The output is:
.*
(dot asterisk) by default is greedy, you need non-greedy, so use left Parentheses dot asterisk right Parentheses(.*?)
you need escape brackets
[]
, see post here: Escape function for regular expression or LIKE patternsyou don’t want capturing brackets
[]
, so you need non-capturing. see: https://www.postgresql.org/docs/current/functions-matching.htmlso you need
(?:re)
SETOF text[]
is output data types for regexp_matches, so you need do array subscript in the end.