Re: Substring RegExp Extract path

From: "Jim Wilson" <jimw(at)kelcomaine(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Substring RegExp Extract path
Date: 2004-08-26 15:08:55
Message-ID: twig.1093532935.84549@kelcomaine.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joe Conway said:

> Nick wrote:
> > "/help/support/index/time.jsp"
> >
> > and i want to extract the 1st, 2nd and 3rd parts
> >
> > 1st = help, 2nd = support, 3rd = index
>
> Are you using 7.4? If so:
>
> select split_part('/help/support/index/time.jsp','/',2),
> split_part('/help/support/index/time.jsp','/',3),
> split_part('/help/support/index/time.jsp','/',4),
> split_part('/help/support/index/time.jsp','/',5);
> split_part | split_part | split_part | split_part
> ------------+------------+------------+------------
> help | support | index | time.jsp
> (1 row)
>
> See:
> http://www.postgresql.org/docs/current/static/functions-string.html
>
> On Postgres 8.0.0beta1 you could also do:
>
> select s.f[2], s.f[3], s.f[4]
> from (select string_to_array('/help/support/index/time.jsp','/') as f)
> as s;
> f | f | f
> ------+---------+-------
> help | support | index
> (1 row)
>

Or if using the substring function in earlier versions:

select substring('/help/support/index/time.jsp','/(.*)/.*/.*/.*'),
substring('/help/support/index/time.jsp','/.*/(.*)/.*/.*'),
substring('/help/support/index/time.jsp','/.*/.*/(.*)/.*'),
substring('/help/support/index/time.jsp','/.*/.*/.*/(.*)');

substring | substring | substring | substring
-----------+-----------+-----------+-----------
help | support | index | time.jsp
(1 row)

Best regards,

Jim

Browse pgsql-general by date

  From Date Subject
Next Message Tino Wildenhain 2004-08-26 15:13:42 Re: space taken by a row & compressed data
Previous Message Bruce Momjian 2004-08-26 15:05:15 Re: space taken by a row & compressed data