Re: regexp_matches question

Поиск
Список
Период
Сортировка
От David Johnston
Тема Re: regexp_matches question
Дата
Msg-id 4BE7AD70-1BE9-4498-BA54-5858F0D49B61@yahoo.com
обсуждение исходный текст
Ответ на regexp_matches question  (Sergio Basurto <sbasurto@soft-gator.com>)
Ответы Re: regexp_matches question  (Sergio Basurto <sbasurto@soft-gator.com>)
Re: regexp_matches question  (Sergio Basurto <sbasurto@soft-gator.com>)
Список pgsql-general
On Sep 4, 2012, at 21:39, Sergio Basurto <sbasurto@soft-gator.com> wrote:

> I am using regexp_matches in a function like this
>
> create or replace function test (v_string       in text) returns varchar as $$
> declare
> i_strings               text[];
> i_string                text[];
>
> i_strings := regexp_matches(v_string,E'[a-zA-Z0-9:\\s\\-\\.#%]*:[A-Za-z0-9\\s\\-\\.#%]+','g');

You can store a single array value into i_strings.  It does not magically convert a multi-row result into an array.
Youcan use ARRAY_AGG to do so or execute the query directly as part of the loop while using a "record" variable to
storethe current row's value(s).  

>
> -- Then I use  the results
> foreach i_string slice 1 in array i_strings
> loop
>         raise notice 'row = %',i_string;
> end loop;
>
> when I run the function like this:
>
> select test('1:Warehouse1;2:Warehouse2;');
>
> postgresql complains:
> ERROR:  query "SELECT regexp_matches(v_string,E'[a-zA-Z0-9:\\s\\-\\.#%]*:[A-Za-z0-9\\s\\-\\.#%]+','g')" returned more
thanone row 
>
> Why postgres is sending the ERROR?
>
> Off course I am expecting more than one row!, that's why is in a foreach loop in the first place.
>
> If I run:
> select regexp_matches('1:Warehouse1;2:Warehouse2;',E'[a-zA-Z0-9:\\s\\-\\.#%]*:[A-Za-z0-9\\s\\-\\.#%]+','g');
> regexp_matches
> ----------------
> {1:Warehouse1}
> {2:Warehouse2}
> (2 rows)
>
> I am doing something wrong?

Note that because you do not use grouping in your expression there is only a single array "cell" in each row - but
therecould be more than one in which case your for-each above would effectively loop through each sub-component of the
match.

>
> Regards,
>

David J.

В списке pgsql-general по дате отправления:

Предыдущее
От: Sergio Basurto
Дата:
Сообщение: regexp_matches question
Следующее
От: Aleksey Tsalolikhin
Дата:
Сообщение: Re: postgres process got stuck in "notify interrupt waiting" status