Re: Count occurrences of pattern in string

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Count occurrences of pattern in string
Date: 2009-10-20 17:24:52
Message-ID: 20091020172452.GA10593@tux
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

dario(dot)ber(at)libero(dot)it <dario(dot)ber(at)libero(dot)it> wrote:

> Hello,
>
> I'm looking for a function to count the occurrences of a pattern in a
> string. E.g. something like:
>
> fun_count_pattern('fooXblaX', 'X')
>
> which would
> return 2 (I.e. pattern 'X' found 2 times in string 'fooXblaX').

How about:

test=*# select length('fooXblaX') - length(regexp_replace('fooXblaX','X','','g')) / length('X');
?column?
----------
2
(1 Zeile)

Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2009-10-20 17:29:07 Re: PostgreSQL driver for Joomla review
Previous Message Wolfgang Keller 2009-10-20 17:11:53 Re: multi-company design/structure ?