Skip to content
Advertisement

Python re.findall regex and text processing

I’m looking to find and modify some sql syntax around the convert function. I want basically any convert(A,B) or CONVERT(A,B) in all my files to be selected and converted to B::A.

So far I tried selecting them with re.findall(r"bconvertb(.*?,.*)", l, re.IGNORECASE) But it’s only returning a small selection out of what I want and I also have trouble actually manipulating the A/B I mentioned.

For example, a sample line (note the nested structure here is irrelevant, I’m only getting the outer layer working if possible)

JavaScript

…should become…

JavaScript

Example2:

JavaScript

…should become…

JavaScript

I think this should be possible with some kind of re.sub with groups and currently have a code structure inside a for each loop where line is the each line in the file:

JavaScript

Edit: In the end I went with a non re solution and handled each line by identifying each block and manipulate them accordingly.

Advertisement

Answer

Here’s my solution based on @Иван-Балван‘s code. Breaking this structure into blocks makes further specification a lot easier than I previously thought and I’ll be using this method for a lot of other operations as well.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement