[英]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 ); 一些文本....... \\ n按范围划分(STRT_DTTM)\\ n更多文字...... \\ 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'.
并用'THIS IS TEST'替换该块。 I am using the below code import re
我使用下面的代码导入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. 你能告诉我出错的地方吗?
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
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 .
然后你将不得不使用
(?s)
修饰符来启用.
matching a new line. 匹配一条新线。
Sample python codes, 示例python代码,
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.