Re: back references using regex

From: Matthew Peter <survivedsushi(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: back references using regex
Date: 2005-09-08 18:03:10
Message-ID: 20050908180311.7827.qmail@web35201.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

That doesn't seem to work with digits

SELECT substring('12.00.00.34' FROM $$((\d+)\.\2)$$);
or
SELECT substring('12.00.00.34' FROM $$((\w+)\.\2)$$);

but works with strings

SELECT substring('abc.foo.foo.xyz' FROM
$$((\w+)\.\2)$$);

What I basically want to do is have a slice function
like Python, where I can slice out items from a \s, \.
or \n\n separated list.

Where I could start it at a certain point and end it
at another. Like slicing out paragraph 3-6 (delimiter
\n\n) or the 2nd-6th sentence in a article (delimiter
\.). That is what I am trying to do. I know if I can
figure how to get that working I can figure out how to
deal with extending it/handling misc. characters/etc
in pgsql. I only need a working vanilla example.
Thanks for all you help.

--- Michael Fuhr <mike(at)fuhr(dot)org> wrote:
> SELECT substring('abc.foo.foo.xyz' FROM
> '(([[:alpha:]]+)\\.\\2)');
> substring
> -----------
> foo.foo
> (1 row)
>
> That is, one or more alphabetic characters followed
> by a dot followed
> by the same set of characters (this is a simplistic
> example: it would
> also match 'foo.oog' and return 'oo.oo').
>
> Note that the back reference is \2 because it refers
> to the inner
> set of parentheses (i.e., the subexpression with the
> second opening
> parenthesis); the outer set is used here for
> capturing. And again,
> note the escaped backslashes because we're using
> ordinary quotes.
> With dollar quotes the above query would be:
>
> SELECT substring('abc.foo.foo.xyz' FROM
> $$(([[:alpha:]]+)\.\2)$$);
>
> --
> Michael Fuhr
>

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Cristian Prieto 2005-09-08 18:16:36 Re: Email Verfication Regular Expression
Previous Message Tom Lane 2005-09-08 18:01:11 Re: [GENERAL] 8.1beta timezone question