Skip site navigation (1) Skip section navigation (2)

Re: writing new regexp functions

From: Jeremy Drake <pgsql(at)jdrake(dot)com>
To: David Fetter <david(at)fetter(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: writing new regexp functions
Date: 2007-02-02 08:15:15
Message-ID: Pine.BSO.4.64.0702020011160.28908@resin.csoft.net (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-patches
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

In response to

Responses

pgsql-hackers by date

Next:From: NikhilSDate: 2007-02-02 08:28:51
Subject: --enable-debug does not work with gcc
Previous:From: Tom LaneDate: 2007-02-02 08:06:19
Subject: Re: Function proposal to find the type of a datum

pgsql-patches by date

Next:From: Jeremy DrakeDate: 2007-02-02 08:54:30
Subject: Re: writing new regexp functions
Previous:From: David FetterDate: 2007-02-02 06:55:18
Subject: Re: writing new regexp functions

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group