On Thu, 1 Feb 2007, David Fetter wrote:

> Yes, although it might have the same name, as in regex_match(pattern
> TEXT, string TEXT, return_pre_and_post BOOL).
>
> The data structure could be something like
>
> TYPE matches (
>     prematch TEXT,
>      match    TEXT[],
>      postmatch TEXT
> )

I just coded up for this:

CREATE FUNCTION regexp_matches(IN str text, IN pattern text) RETURNS
text[]
    AS 'MODULE_PATHNAME', 'regexp_matches'
    LANGUAGE C IMMUTABLE STRICT;

CREATE FUNCTION regexp_matches(
        IN str text, IN pattern text, IN return_pre_and_post bool,
        OUT prematch text, OUT fullmatch text, OUT matches text[], OUT
postmatch text) RETURNS record
    AS 'MODULE_PATHNAME', 'regexp_matches'
    LANGUAGE C IMMUTABLE STRICT;


Which works like this:

jeremyd=# \pset null '\\N'
Null display is "\N".
jeremyd=# select * from regexp_matches('foobarbequebaz',
$re$(bar)(beque)$re$);
 regexp_matches
----------------
 {bar,beque}
(1 row)

jeremyd=# select * from regexp_matches('foobarbequebaz',
$re$(bar)(beque)$re$, false);
 prematch | fullmatch |   matches   | postmatch
----------+-----------+-------------+-----------
 \N       | \N        | {bar,beque} | \N
(1 row)

jeremyd=# select * from regexp_matches('foobarbequebaz',
$re$(bar)(beque)$re$, true);
 prematch | fullmatch |   matches   | postmatch
----------+-----------+-------------+-----------
 foo      | barbeque  | {bar,beque} | baz
(1 row)


And then you also have this behavior in the matches array:

jeremyd=# select * from regexp_matches('foobarbequebaz',
$re$(bar)(.*)(beque)$re$);
 regexp_matches
----------------
 {bar,"",beque}
(1 row)

jeremyd=# select * from regexp_matches('foobarbequebaz',
$re$(bar)(.+)(beque)$re$);
 regexp_matches
----------------
 \N
(1 row)

jeremyd=# select * from regexp_matches('foobarbequebaz',
$re$(bar)(.+)?(beque)$re$);
  regexp_matches
------------------
 {bar,NULL,beque}
(1 row)

Reasonable?

-- 
A.A.A.A.A.:
        An organization for drunks who drive

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Reply via email to