"Daniel Verite" <daniel@xxxxxxxxxxxxxxxx> 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