CREATE OR REPLACE FUNCTION regexp_positions(string text, pattern text, OUT start_pos integer, OUT end_pos integer)
RETURNS SETOF RECORD
LANGUAGE plpgsql
AS
$$
DECLARE
match text;
remainder text := string;
len integer;
pos integer;
BEGIN
end_pos := 0;
FOR match IN SELECT (regexp_matches(string,format('(%s)',pattern),'g'))[1] LOOP
  len := length(match);
  pos := position(match in remainder);
  start_pos := pos + end_pos;
  end_pos := start_pos + len - 1;
  RETURN NEXT;
  remainder := right(remainder, 1 - len - pos);
END LOOP;
RETURN;
END
$$;
