Re: Regex substring help - Mailing list pgsql-general

From David Fetter
Subject Re: Regex substring help
Date
Msg-id 20090903230021.GA6128@fetter.org
Whole thread Raw
In response to Regex substring help  (Nick <nboutelier@gmail.com>)
List 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@fetter.org> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: david.fetter@gmail.com

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

pgsql-general by date:

Previous
From: Nick
Date:
Subject: Regex substring help
Next
From: "Joshua D. Drake"
Date:
Subject: Re: Problem with leaking connections