Re: regexp_replace double quote

From: Михаил <m(dot)nasedkin(at)gmail(dot)com>
To: depesz(at)depesz(dot)com
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: regexp_replace double quote
Date: 2016-08-17 17:46:34
Message-ID: CALSKcLQ_WcOBX2yxuQrmoMxz+oKyEzvSUW3GysJGK73D+CVPNA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thank you!

2016-08-15 18:36 GMT+05:00, hubert depesz lubaczewski <depesz(at)depesz(dot)com>:
> On Mon, Aug 15, 2016 at 06:27:06PM +0500, Михаил wrote:
>> I need to escape double quotes only:
>> test=# select regexp_replace('"""{Performer,"Boomwacker ""a""
>> Recording""}"""', '([^"])"{2}([^"])', '\1\"\2', 'g');
>> regexp_replace
>> -------------------------------------------------
>> """{Performer,"Boomwacker \"a"" Recording\"}"""
>>
>> This is unexpected result.
>>
>> But when added one symbol to ""a"" the result is right:
>> test=# select regexp_replace('"""{Performer,"Boomwacker ""a1""
>> Recording""}"""', '([^"])"{2}([^"])', '\1\"\2', 'g');
>> regexp_replace
>> --------------------------------------------------
>> """{Performer,"Boomwacker \"a1\" Recording\"}"""
>
> This is because when finding first "", "a" that is afterwards get
> assigned to \2. and thus is already "used", and can't be part of
> match for the second "".
>
> What will solve the problem is to use lookahead, like:
> $ select regexp_replace('"""{Performer,"Boomwacker ""a"" Recording""}"""',
> '([^"])"{2}(?=[^"])', '\1\"', 'g');
> regexp_replace
> -------------------------------------------------
> """{Performer,"Boomwacker \"a\" Recording\"}"""
> (1 row)
>
> because then the part inside (?=...) is not "used", and can be used for
> next
> match.
>
> Not sure if I'm clear, but hopefully you'll understand what I'm trying to
> explain :)
>
> Best regards,
>
> depesz
>
>

--
---
Regards,

Mikhail

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gavin Flower 2016-08-17 18:14:40 Re: [GENERAL] C++ port of Postgres
Previous Message Robert Haas 2016-08-17 16:21:03 Re: [GENERAL] C++ port of Postgres