Re: substring and POSIX re's

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Don Isgitt <djisgitt(at)soundenergy(dot)com>
Cc: postgresql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: substring and POSIX re's
Date: 2005-04-19 14:53:00
Message-ID: 21596.1113922380@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Don Isgitt <djisgitt(at)soundenergy(dot)com> writes:
> gds2=# select substring('NE NE SE 2310 FSL 330 FEL' from '^([A-Z][A-Z] )+');
> substring
> -----------
> SE
> (1 row)

> The pg docs say that this form of substring uses POSIX re's, and my
> understanding of POSIX re's is they are always greedy. So, why do I get
> only SE instead of NE NE SE? Pilot error, probably, but would someone
> please enlighten me? Thank you very much.

I think you want

regression=# select substring('NE NE SE 2310 FSL 330 FEL' from '^(([A-Z][A-Z] )+)');
substring
-----------
NE NE SE
(1 row)

ie, you need the "+" to be *inside* the capturing parentheses. When
it's outside, I guess the engine chooses to consider the last match
of the parenthesized subexpression as the thing to return. (I can't
recall if this choice is specified in the docs or not.)

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Benjamin Holmberg 2005-04-19 15:01:26 Datatypes in PL/PSQL functions with multiple arguments
Previous Message Harald Fuchs 2005-04-19 14:43:20 Finding FOREIGN KEY constraints via information_schema