[Solved] python replace string at 2 parts in one line [duplicate]


You can use the Python replacement regex engine to do recursion and
the replacement you want.

Regex r"LTRIM\(RTRIM(\((?:(?>(?!LTRIM\(RTRIM\(|[()])[\S\s])+|\(|(?R))*\))\)"
Replace with r"TRIM\1"

Sample

import regex

src=""'
.. LTRIM(RTRIM(AB.ITEM_ID)) AS ITEM_NUMBER,
 .. REPLACE(LTRIM(RTRIM(NVL(AB.ITEM_SHORT_DESC,AB.ITEM_DESC))),',','') AS SHORT_DESC
.. LTRIM(RTRIM(AB.ITEM_ID))** AS ITEM_NUMBER,**
'''

srcnew = regex.sub(r"LTRIM\(RTRIM(\((?:(?>(?!LTRIM\(RTRIM\(|[()])[\S\s])+|\(|(?R))*\))\)", r"TRIM\1", src)

print( srcnew )

see https://repl.it/repls/DelightfulSatisfiedCore#main.py

1

solved python replace string at 2 parts in one line [duplicate]