Re: SELECT substring with regex

From: "Jim Buttafuoco" <jim(at)contactbda(dot)com>
To: mailreg(at)numerixtechnology(dot)de, pgsql-sql(at)postgresql(dot)org
Subject: Re: SELECT substring with regex
Date: 2006-07-07 20:34:32
Message-ID: 20060707203420.M77919@contactbda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

use plperl

---------- Original Message -----------
From: T E Schmitz <mailreg(at)numerixtechnology(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Sent: Fri, 07 Jul 2006 20:23:50 +0100
Subject: Re: [SQL] SELECT substring with regex

> Rodrigo De Leon wrote:
> > On 7/7/06, T E Schmitz <mailreg(at)numerixtechnology(dot)de> wrote:
> >
> >> But that takes me to the next problem:
> >>
> >> For the sake of the example I simplified the regular pattern.
> >> In reality, BASE_NAME might be:
> >>
> >> 28mm
> >> 28-70mm
> >>
> >> So the reg. expr. requires brackets:
> >>
> >> substring (NAME, '^(\\d+(-\\d+)?mm)' ) as BASE_NAME
> >>
> >> Actually, the pattern is more complex than that and I cannot see how I
> >> can express it without brackets.
> >
> >
> > Maybe:
> >
> > select
> > substring ('28-70mm LD AD Asp XR Macro', '^[\\d-]*mm' ) as BASE_NAME
>
> Sorry, but that would also capture something like
> 10-30-59mm
>
> The pattern describes either a single length (120 millimeters) or a
> range (30 to 70 millimetres), hence:
>
> \\d+(-\\d+)?mm
>
> The ? quantifier refers to the combination of '-' and digits and has to
> be bracketed.
>
> If the brackets cannot be avoided in the expression, your original
> suggestion might come in handy though:
>
> SELECT
> substring (NAME, '^\\d+(-\\d+)?mm') AS BASE_NAME ,
> substr(
> NAME
> , char_length(
> substring (NAME, '^\\d+(-\\d+)?mm')
> ) + 2
> ) AS SUFFIX
>
> Still, I'd be interested to know whether there is a 'more elegant' solution.
>
> --
>
> Regards,
>
> Tarlika Elisabeth Schmitz
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
------- End of Original Message -------

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message T E Schmitz 2006-07-07 20:49:09 Re: SELECT substring with regex
Previous Message T E Schmitz 2006-07-07 20:12:52 Re: SELECT substring with regex