Forums

Run multiple regex (find and replace) in files

hello. 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']

We can help you with PythonAnywhere-related problems, but you need to look for help on more general forums for general questions like that.

I made a Python cod for regex, but has a little error:

Traceback (most recent call last): File "D:\11\bebe.py", line 21, in <module> print(re.sub(k, v, 'text')) NameError: name 're' is not defined

This is the code:

import os
import re
path = "D:/11"

def listdir(dir):
    filenames = os.listdir(dir)
    for files in filenames:
        print(files)

listdir(path)


extensie_fisier = ".html"

regexes = {
    r'<title>.*\|\K(.*)(</title>)': r'Bebe',  #  First regex     r'regex1': r'string1'
    r'^<html.*$': r'yes'                      #  Second regex          r'regex1': r'string1'
}

for k, v in regexes.items():
    print(re.sub(k, v, 'text'))


with open("*.html", mode="w") as file:  # See note 2.
    contents = file.write(f"{extensie_fisier.text}")

print("DONE !")

You need to import re if you want to use it.