"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).
BTW, just to think outside the box a bit, I wonder whether you
couldn't build this out of regexp_split_to_array:
regression=# select regexp_split_to_array('junkfoolbarfoolishfoobarmore', 'foo|bar|foobar');
regexp_split_to_array
-----------------------
{junk,l,"",lish,more}
(1 row)
The idea would be to iterate over the array elements, tracking the
corresponding position in the source string, and re-discovering at
each break which of the original alternatives must've matched.
It's sort of annoying that we don't have a simple "regexp_location"
function that would give you back the starting position of the
first match.
regards, tom lane