简体   繁体   English

替换文件中的所有正则表达式匹配项

[英]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导致输入文件被移动到备份文件并且标准输出被定向到输入文件,如可选就地过滤中所述。

You can use Notepad++ with Version >= 6.0.您可以使用版本 >= 6.0 的Notepad++ Since then it does support PCRE Regex.从那时起它就支持 PCRE 正则表达式。

You can then use your regex a(.+?)a and replace with $1然后,您可以使用正则表达式a(.+?)a并替换为$1

sed sed

Are you limited to using Python tools?您是否仅限于使用 Python 工具? Because sed works very well.因为sed工作得很好。

$ sed -i <filename> "s/a(.+?)a/\1/g"

Vim维姆

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

Python Python

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM