[英]Replace all regex matches in a file
Consider a basic regex like a(.+?)a
.考虑像
a(.+?)a
这样的基本正则表达式。 How can one replace all occurences of that regex in a file with the content of the first group?如何用第一组的内容替换文件中该正则表达式的所有出现?
Use can use the re
module to use regular expressions in python and the fileinput
module to simply replace text in files in-place可以使用
re
模块在 python 中使用正则表达式,使用fileinput
模块简单地就地替换文件中的文本
Example:例子:
import fileinput
import re
fn = "test.txt" # your filename
r = re.compile('a(.+?)a')
for line in fileinput.input(fn, inplace=True):
match = r.match(line)
print match.group() if match else line.replace('\n', '')
Before:前:
hello this
你好这个
aShouldBeAMatch!!!!!aShouldBeAMatch!!!!! and this should be gone
这应该消失了
you know你知道
After:后:
hello this
你好这个
aShouldBeAMa一个应该是妈妈
you know你知道
Note: this works because the argument inplace=True
causes input file to be moved to a backup file and standard output is directed to the input file , as documented under Optional in-place filtering .注意:这是有效的,因为参数
inplace=True
导致输入文件被移动到备份文件并且标准输出被定向到输入文件,如可选就地过滤中所述。
Are you limited to using Python tools?您是否仅限于使用 Python 工具? Because
sed
works very well.因为
sed
工作得很好。
$ sed -i <filename> "s/a(.+?)a/\1/g"
In a Vim window, give the following search-and-replace ex command:在 Vim 窗口中,提供以下搜索和替换 ex 命令:
:%s/\va(.+?)a/\1/g
Note that many regex characters are escaped in Vim- \v
sets "very magic" mode, which removes the need for escaping.请注意,许多正则表达式字符在 Vim 中被转义 -
\v
设置“非常神奇”模式,这消除了转义的需要。 The same command with "magic" (the default) is :%s/a\(.\+\?)a/\1/g
带有“magic”(默认)的相同命令是
:%s/a\(.\+\?)a/\1/g
If you're looking to do this in Python, BigYellowCactus' answer is excellent (use the re
module for regex, and fileinput
to modify the file).如果您想在 Python 中执行此操作,BigYellowCactus 的答案非常好(使用正则表达式的
re
模块,并fileinput
修改文件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.