Re: Regex substring help

From: David Fetter <david(at)fetter(dot)org>
To: Nick <nboutelier(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regex substring help
Date: 2009-09-03 23:00:21
Message-ID: 20090903230021.GA6128@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Sep 03, 2009 at 03:22:12PM -0700, Nick wrote:
> Im trying to get all the text before the '<br>' tag.
>
> SELECT SUBSTRING('one<br>two<br>three','(^.*)<br>.*$');
>
> returns "one<br>two"
>
> How do I get it to return "one"?

You can either use a non-greedy regex like this:

SELECT substring('one<br>two<br>three','(^.*?)<br>.*$');

Note the '?' after the '*'. That makes it non-greedy.

Another way to do this would be with string_to_array:

SELECT (string_to_array('one<br>two<br>three','<br>'))[1];

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Condit 2009-09-03 23:21:48 Re: query speed question
Previous Message Nick 2009-09-03 22:22:12 Regex substring help