简体   繁体   中英

PSQL Regex match transforming many to one

How can I transform the results of this query:

select regexp_matches('number: 772392 number: 911604 number:123456',  '[0-9]{6}', 'g');

From this:

{772392}
{911604}
{123456}

To this?:

{772392,911604,123456}

I've tried transforming it via array_to_string , and array_agg , but I haven't been successful.

select array_to_string
(array_agg(regexp_matches
('number: 772392 number: 911604 number:123456', '[0-9]{6}', 'g')), ', ');

regexp_matches returns set of text arrays

If the pattern contains no parenthesized subexpressions, then each row returned is a single-element text array containing the substring matching the whole pattern.

so you should

select string_agg(numb[1], ', ') 
    from regexp_matches('number: 772392 number: 911604 number:123456', '[0-9]{6}', 'g')
    as numb;

to achieve your goals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM