Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用python正则表达式执行多个文本重放时使用循环操作_Python_Regex - Fatal编程技术网

如何在使用python正则表达式执行多个文本重放时使用循环操作

如何在使用python正则表达式执行多个文本重放时使用循环操作,python,regex,Python,Regex,“”“如何借助循环操作替换上述代码?” 输入文件有旧的_文本,后缀为数字,必须 替换为带有相应数字作为后缀的新文本。这是一个 手动方法工作正常,但不是自动方法“”如果我了解问题。。。这应该有帮助:) 如果搜索和替换文本确实遵循示例中的模式,则只需要一个re.sub()调用: import re in_file=open('INPUT_FILE.txt','r+') out_file=open('OUTPUT_FILE.txt','w') patterns = [(r'OLD_TEXT_01',

“”“如何借助循环操作替换上述代码?” 输入文件有旧的_文本,后缀为数字,必须 替换为带有相应数字作为后缀的新文本。这是一个
手动方法工作正常,但不是自动方法“”

如果我了解问题。。。这应该有帮助:)


如果搜索和替换文本确实遵循示例中的模式,则只需要一个
re.sub()
调用:

import re

in_file=open('INPUT_FILE.txt','r+')
out_file=open('OUTPUT_FILE.txt','w')
patterns = [(r'OLD_TEXT_01','NEW_TEXT_01'),(r'OLD_TEXT_02','NEW_TEXT_02'), (r'OLD_TEXT_03','NEW_TEXT_03'), (r'OLD_TEXT_04','NEW_TEXT_04'), (r'OLD_TEXT_05','NEW_TEXT_05')]

# OPTION 1: Loop to read input file line by line
for line in in_file.redlines():
  for pattern, replacement in patterns:
    line = re.sub(pattern, replacement, line)
  out_file.write(line)

# OPTION 2: Alternative loop that reads in the whole input file (not line by line)
# NOT ADVISABLE FOR LARGE FILES (takes lots of space in memory)
out_put = in_file.read()
for pattern, replacement in patterns:
  out_put = re.sub(pattern, replacement, out_put, re.M)
out_file.write(out_put)


#Close input and output
out_file.close()
in_file.close()

在替换文本中使用的组中捕获可变编号部件。并将
flags
参数与您的代码进行比较:它必须是第5个位置参数或作为关键字参数提供。

输入和输出示例请参见为什么将“旧文本”替换为“新文本”五次?仅仅做一次是不够的吗?您可能不知道,但是re.sub会替换所有匹配项。@Jerry没有像上面写的那样,因为
re.M
的值为8,并且在示例中用作
count
参数,但最多只能进行8次替换。;-)@21点哇,这对我来说是新鲜事!我简直不敢相信我所看到的……在Python中,
对于范围内的I(len(sequence)):
是一种反模式。您可以直接在元素上迭代,而无需间接索引:
对于模式,在模式中替换:
重写循环以使BlackJack开心:)
import re

in_file=open('INPUT_FILE.txt','r+')
out_file=open('OUTPUT_FILE.txt','w')
patterns = [(r'OLD_TEXT_01','NEW_TEXT_01'),(r'OLD_TEXT_02','NEW_TEXT_02'), (r'OLD_TEXT_03','NEW_TEXT_03'), (r'OLD_TEXT_04','NEW_TEXT_04'), (r'OLD_TEXT_05','NEW_TEXT_05')]

# OPTION 1: Loop to read input file line by line
for line in in_file.redlines():
  for pattern, replacement in patterns:
    line = re.sub(pattern, replacement, line)
  out_file.write(line)

# OPTION 2: Alternative loop that reads in the whole input file (not line by line)
# NOT ADVISABLE FOR LARGE FILES (takes lots of space in memory)
out_put = in_file.read()
for pattern, replacement in patterns:
  out_put = re.sub(pattern, replacement, out_put, re.M)
out_file.write(out_put)


#Close input and output
out_file.close()
in_file.close()
import re


def main():
    with open('INPUT_FILE.txt', 'r') as in_file:
        content = in_file.read()

    new_content = re.sub(
        r'OLD_TEXT_0([1-5])', r'NEW_TEXT_0\1', content, flags=re.MULTILINE
    )

    with open('OUTPUT_FILE.txt', 'w') as out_file:
        out_file.write(new_content)


if __name__ == '__main__':
    main()