pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedselect regexp_matches('a a a', '([a-z]) a','g');
2+ messages / 2 participants
[nested] [flat]
* select regexp_matches('a a a', '([a-z]) a','g');
@ 2009-05-07 14:51 Marc Mamin <M.Mamin@intershop.de>
0 siblings, 0 replies; 2+ messages in thread
From: Marc Mamin @ 2009-05-07 14:51 UTC (permalink / raw)
To: pgsql-sql
Hello,
I wonder if someone has an idea for this problem:
I have a string that contains a serie of chars, separated by single
spaces.
e.g 'a b x n r a b c b'
Having such a string, I d'like to get a list of all predecessors of a
given character.
In the example, the predecessors of b are a,a,c.
If I now have the string 'a a a', the predecessors of 'a' are a,a
I tried to use regexp_matches for this:
select regexp_matches('a a a', '([a-z]) a','g');
=> {"a "} only
As the second parameter of the function matches the first 2 'a',
only the trailing ' a' will be used to seek for further matching...
Cheers,
Marc Mamin
^ permalink raw reply [nested|flat] 2+ messages in thread
* select regexp_matches('a a a', '([a-z]) a','g');
@ 2009-05-08 05:26 Craig Ringer <craig@postnewspapers.com.au>
0 siblings, 0 replies; 2+ messages in thread
From: Craig Ringer @ 2009-05-08 05:26 UTC (permalink / raw)
To: pgsql-sql
First: Please don't reply to an existing message to create a new thread.
Your mail client copies the replied-to message ID into the References:
header, and well-implemented mail clients will thread your message under
a now-unrelated thread.
Compose a new message instead.
Marc Mamin wrote:
> I have a string that contains a serie of chars, separated by single
> spaces.
>
> e.g 'a b x n r a b c b'
>
> Having such a string, I d'like to get a list of all predecessors of a
> given character.
> In the example, the predecessors of b are a,a,c.
OK, so wherever `b' occurs, you want the character at index `b -2'.
> select regexp_matches('a a a', '([a-z]) a','g');
> => {"a "} only
The issue is that regular expressions don't like to overlap matches. The
first match consumes _two_ leading `a' characters.
What you need is a zero-width lookahead assertion, available in
Perl-style extended regular expressions. Handily, recent PostgreSQL
versions support these, so you can write:
test=> select regexp_matches( 'a a a', '([a-z]) (?=a)', 'g');
regexp_matches
----------------
{a}
{a}
(2 rows)
--
Craig Ringer
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2009-05-08 05:26 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-05-07 14:51 select regexp_matches('a a a', '([a-z]) a','g'); Marc Mamin <M.Mamin@intershop.de>
2009-05-08 05:26 select regexp_matches('a a a', '([a-z]) a','g'); Craig Ringer <craig@postnewspapers.com.au>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox