Re: regexp_replace to remove sql comments

From: Marc Mamin <M(dot)Mamin(at)intershop(dot)de>
To: Mike <mike(at)wolman(dot)co(dot)uk>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: regexp_replace to remove sql comments
Date: 2015-10-28 22:43:18
Message-ID: B6F6FD62F2624C4C9916AC0175D56D884209EB3E@jenmbs01.ad.intershop.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


>________________________________________
>Von: pgsql-general-owner(at)postgresql(dot)org [pgsql-general-owner(at)postgresql(dot)org]&quot; im Auftrag von &quot;Mike [mike(at)wolman(dot)co(dot)uk]
>Gesendet: Mittwoch, 28. Oktober 2015 20:04
>An: pgsql-general(at)postgresql(dot)org
>Betreff: [GENERAL] regexp_replace to remove sql comments
>
>Hi,
>
>I am trying to clean up the query field returned by the
>pg_stat_statements extension and remove all comments.
>
>Some of the queries in the query field contain comments like '-- some
>comment' and also '/* c style comments */'
>
>I have managed to strip off the '--' comments and also white space but
>after trying numerous regex for this via google but I am stuck.
>
>WITH to_clean AS (
> SELECT
> regexp_replace(
> regexp_replace(trim(query), '--[^\r\n]*', '') --clear up
>comments like this one <-- this is ok
> , '\s+', ' ', 'g') as q --clear up white space <-- this is ok
> FROM public.pg_stat_statements
> WHERE dbid IN (SELECT oid FROM pg_database WHERE datname =
>current_database())
>)
>
>SELECT regexp_replace(q,'/\*.*\*/','') as q /* strip off comments like
>this */ <-- cannot get a regex to do this
>FROM to_clean ORDER BY q

Hi,
Does this help ?

select regexp_replace(' aaa /*
x
y
z
*/ foo', '\/\*.+\*\/','','g'):

regards,

Marc Mamin

>
>Im now thinking it may be better to do in a pgsql function as I think if
>the comments are in queries then they need to be ignored.
>
>Has anyone done anything like this?
>
>Thanks,
>
>Mike.
>
>
>
>--
>Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
>To make changes to your subscription:
>http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mike 2015-10-28 22:58:34 Re: regexp_replace to remove sql comments
Previous Message Kevin Grittner 2015-10-28 22:26:00 Re: Postgresql SSI: read/write dependencies