Re: Regex substring help

Поиск
Список
Период
Сортировка
От David Fetter
Тема Re: Regex substring help
Дата
Msg-id 20090903230021.GA6128@fetter.org
обсуждение исходный текст
Ответ на Regex substring help  (Nick <nboutelier@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: Nick
Дата:
Сообщение: Regex substring help
Следующее
От: "Joshua D. Drake"
Дата:
Сообщение: Re: Problem with leaking connections