Imtiyaz Ali
Imtiyaz Ali

Reputation: 81

Multiline replace with python regular expression

I have a repeating text in a large file which I want to replace with some other text. For example:

some text.......\n partition by range (STRT_DTTM)\n some more text......\n ); I want to use regex to find these blocks that start with partition by range and ends with ); and replace that block with 'THIS IS TEST'. I am using the below code import re

with open(r"C:\Users\x217838\Desktop\python\input.txt","rt") as in_file:
    text = in_file.read()
    s = re.compile("^partition by range(.*);\)$)",re.MULTILINE)
    replace = re.sub(s, 'THIS IS TEST', text)
    print(replace)

Can you please let me know where I am going wrong.

Upvotes: 1

Views: 161

Answers (2)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

If you have your text spanning across multiple lines something like this,

some text.......
partition by range (STRT_DTTM)
some more text......
);

Then you will have to use (?s) modifier to enable . matching a new line.

Demo

Sample python codes,

import re

s = '''some text.......
partition by range (STRT_DTTM)
some more text......
);'''

mods = re.sub(r'(?s)partition by range(.*?)\);','THIS IS TEST',s)
print(mods)

Prints,

some text.......
THIS IS TEST

Upvotes: 1

Marco Visibelli
Marco Visibelli

Reputation: 359

You have to use \ for all regex reserved symbols --> [\^$.|?*+(){}. The final code will be:

import re
text = "partition by range(CANE) uno"
s = re.compile("^partition by range\(.*\)",re.MULTILINE)
replace = re.sub(s, 'THIS IS TEST', text)
print(replace)

The result is:

THIS IS TEST uno

Upvotes: 2

Related Questions