Python Forum
Find and replace in files with regex and Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find and replace in files with regex and Python (/thread-33865.html)



Find and replace in files with regex and Python - Melcu54 - Jun-03-2021

good day. I need to make a simple Batch Processor (run multiple regex for search and replace on files)

Suppose I have 6 or 10 regex formula for search and replace. For example. First 2 regex:

Search: <title>.*\|\K(.*)(</title>)
Replace by: \x20 Test \x20\2

Search: <em>.*\|.*</em>)(</title>)
Replace by: \x20\2

and so one,. The Python code should be run these regex, in the order I choose, and to be able to add always a new regex. But for regex, you have to consider also the option .matches newsline

But if I want to make 3 regex replacement, one by one in the order I want, in some .txt files, how can I do that ?

This is a point of view

import re
    s = '(zyx)bc'
    print (re.findall(r'(?<=\()\w+(?=\))|\w', s))
    ['zyx', 'b', 'c']