Python Forum
Find and replace in files with regex and Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find and replace in files with regex and Python
#1
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex replace in SQLite3 database WJSwan 1 749 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Replace a text/word in docx file using Python Devan 4 2,853 Oct-17-2023, 06:03 PM
Last Post: Devan
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,492 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Find duplicate files in multiple directories Pavel_47 9 2,926 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Python Regex quest 2 2,229 Sep-22-2022, 03:15 AM
Last Post: quest
  Find numbers using Regex giddyhead 18 3,013 Jul-28-2022, 12:29 AM
Last Post: giddyhead
  Find and Replace numbers in String giddyhead 2 1,197 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  python-docx regex: replace any word in docx text Tmagpy 4 2,139 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  count every 28 files and find desire files RolanRoll 3 2,019 Dec-12-2021, 04:42 PM
Last Post: Axel_Erfurt
  Have to use Python 2.x to find "yesterday' files tester_V 6 2,770 Sep-19-2021, 12:26 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020