Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:12:24+00:00 2026-06-11T21:12:24+00:00

I have one project where I need to apply a dozen or so regex

  • 0

I have one project where I need to apply a dozen or so regex to about 100 files using python. 4+ hours of searching the web for various combinations including “(merge|concatenate|stack|join|compile) multiple regex in python” and I haven’t found any posts regarding my need.

This is a mid-sized project for me. There are several smaller regex projects that I need which take only 5-6 regex patterns applied over only a dozen or so files. While these will be a great aid in my work, the grand-daddy project is a applying a file of 100+ search, replace strings to any new file I get. (Spelling conventions in certain languages are not standardized and being able to quick-process files will increase productivity.)

Ideally, the regex strings need to be update-able by a non programmer, but that maybe outside of the scope of this post.

Here is what I have so far:

import os, re, sys # Is "sys" necessary?

path = "/Users/mypath/testData"
myfiles = os.listdir(path)

for f in myfiles:

    # split the filename and file extension for use in renaming the output file
    file_name, file_extension = os.path.splitext(f)
    generated_output_file = file_name + "_regex" + file_extension

    # Only process certain types of files.
    if re.search("txt|doc|odt|htm|html")

    # Declare input and output files, open them, and start working on each line.
        input_file = os.path.join(path, f)
        output_file = os.path.join(path, generated_output_file)

        with open(input_file, "r") as fi, open(output_file, "w") as fo:
            for line in fi:

    # I realize that the examples are not regex, but they are in my real data.
    # The important thing, is that each of these is a substitution.
                line = re.sub(r"dog","cat" , line)
                line = re.sub(r"123", "789" , line)
                # Etc.

    # Obviously this doesn't work, because it is only writing the last instance of line.
                fo.write(line)
                fo.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:12:25+00:00Added an answer on June 11, 2026 at 9:12 pm

    Is this what you’re looking for?

    Unfortunately you didn’t specify how you know which regexes are supposed to be applied, so I put them into a list of tuples (first element is the regex, second is the replacement text).

    import os, os.path, re
    
    path = "/Users/mypath/testData"
    myfiles = os.listdir(path)
    # its much faster if you compile your regexes before you
    # actually use them in a loop
    REGEXES = [(re.compile(r'dog'), 'cat'),
               (re.compile(r'123'), '789')]
    for f in myfiles:
        # split the filename and file extension for use in
        # renaming the output file
        file_name, file_extension = os.path.splitext(f)
        generated_output_file = file_name + "_regex" + file_extension
    
        # As l4mpi said ... if odt is zipped, you'd need to unzip it first
        # re.search is slower than a simple if statement
        if file_extension in ('.txt', '.doc', '.odt', '.htm', '.html'):
    
            # Declare input and output files, open them,
            # and start working on each line.
            input_file = os.path.join(path, f)
            output_file = os.path.join(path, generated_output_file)
    
            with open(input_file, "r") as fi, open(output_file, "w") as fo:
                for line in fi:
                    for search, replace in REGEXES:
                        line = search.sub(replace, line)
                    fo.write(line)
            # both the input and output files are closed automatically
            # after the with statement closes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to have one project on asp.net mvc 1 but i want to
I have one project inside that I have SqlServerFunctions in test.cs file. code: [Microsoft.SqlServer.Server.SqlFunction]
I have one project with is developed in Php and javascript. How to open
I have one project which has RIAService with entity framework that is referenced to
I currently have one project that currently contains multiple packages. These packages make up
Let's say I have one project with the following POM: <groupId>com.mine</groupId> <artifactId>coreJar</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version>
I have one main project and another project which is added as a subprojects.
We have one main project and two subprojects. One of the subprojects is the
I have one java project in netBeans. There are some packages, and classes in
I have created one library project in iphone and my images folder is present

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.