TransWikia.com

Problem to apply replacement with regexp that include look ahead in Postgres

Database Administrators Asked by Randomize on December 19, 2021

I have this kind of string 12s2-3a abc def and I am trying to apply the regexp ^(?=.*d) to remove the first part and having as left abc def.

I have tried with:

select substring ('12s2-3 abc def' from '^(?=.*d)');

and

select regexp_replace ('12s2-3 abc def', '^(?=.*d)', '');

but none of those has worked. The only one that looks working is the match verification (~).

Any idea?

One Answer

Look-ahead are zero width. They don't match anything themselves. So your replacement operations is just replacing the empty string with the empty string, under certain conditions.

Simply removing the look-ahead designation makes it give the answer you want for your one example:

select regexp_replace ('12s2-3 abc def', '^.*d', '');

Answered by jjanes on December 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP