Re: Mixing greediness in regexp_matches

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Mixing greediness in regexp_matches
Дата
Msg-id 28143.1577114140@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Mixing greediness in regexp_matches  ("Daniel Verite" <daniel@manitou-mail.org>)
Ответы Re: Mixing greediness in regexp_matches  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Mixing greediness in regexp_matches  ("Daniel Verite" <daniel@manitou-mail.org>)
Список pgsql-general
"Daniel Verite" <daniel@manitou-mail.org> writes:
> The basic idea is to iterate on the rows produced by
>    regexp_matches(string, '(.*?)(foo|bar|foobar)', 'g')
> to break down the string into pairs of (non-matching segment,
> matching segment) so that a final result can be assembled
> from that (setting aside the last non-matching segment, that
> can be retrieved in a final step).
> The difficulty is that the longest strings in the alternation
> should be prioritized, but the starting (.*?) makes the RE
> non-greedy so "foo" is choosen over "foobar". 

I'd try forcing the match to be the whole string, ie

    ^(.*?)(foo|bar|foobar)(.*)$

which would also save some work for restarting the iteration,
since you'd have already captured the all-the-rest substring.

With the endpoints forced, it doesn't really matter whether
the engine deems the RE-as-a-whole to be greedy or not.
I think this would work without needing any explicit greediness
marking for the second and third parts, but I might be wrong
about that detail.

            regards, tom lane



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

Предыдущее
От: "Daniel Verite"
Дата:
Сообщение: Mixing greediness in regexp_matches
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Mixing greediness in regexp_matches