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

  • Home
  • SEARCH
  • 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 8713689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:24:06+00:00 2026-06-13T05:24:06+00:00

I am making a script to automatically parse some text data (with a complex

  • 0

I am making a script to automatically parse some text data (with a complex structure) and insert it into a MySQL database.

I would like to have multiple for loops that iterate over a list of files based on regex matches to the file names. In the end I will concatenate them and insert them into the database.

Here are my regex expressions:

Trgx= re.compile('([a-zA-Z0-9]{3,4})_.*_.*_.*$');
Dtrgx= re.compile('[a-zA-Z0-9]{3,4}_[a-zA-Z0-9]{3,4}_([0-9]{10})_[0-9]{3}'); 
Mrgx= re.compile('.*_([a-zA-Z0-9]{3,4})_.*$'); 
Hrgx= re.compile('.*([0-9]{3}).csv$'); 

My filenames look like this:

ecd_cdd_2012102100_000.csv
ecd_cdd_2012102100_024.csv
ecd_hdd_2012102200_000.csv
ecd_hdd_2012102200_024.csv
ecd_hdd_2012102200_048.csv
ecd_avgt_2012102200_120.csv
ecd_avgt_2012102200_144.csv
ecd_avgt_2012102200_168.csv
ecd_mint_2012102200_192.csv
ecd_maxt_2012102200_144.csv
ecd_maxt_2012102200_168.csv
ecd_cdd_2012102200_000.csv
ecd_cdd_2012102200_024.csv

Each expression captures a subset of the file name:

  • Trgx captures the first part (“ecd” in every instance in this example)
  • Mrgx captures the second part (“cdd”,”hdd”,”avgt”,etc)
  • Dtrgx captures the date/time segment (like 2012102100)
  • Hrgx captures the last part (like 000 or 024, before the extension)

Every file name will match every regular expression, but .group(1) will be populated
by different values.

I want to iterate through the files using the regex exprssions as “grouping” elements, so that I concatenate them together in the right order.

Something like this:

for fileName in fileNameList
    for each distinct value in  Trgx.group(1)
         for each distinct value in  Dtrgx.group(1)
              for each distinct value in Hrgx.group(1)
                     do whatever
  • 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-13T05:24:07+00:00Added an answer on June 13, 2026 at 5:24 am

    It may be easier to combine the regexes together

    re_fn = re.compile('(?P<T>[a-zA-Z0-9]{3,4})_(?P<M>[a-zA-Z0-9]{3,4})_(?P<Dt>[0-9]{10})_(?P<H>[0-9]{3}).csv')
    

    and save yourself the trouble of maintaining four regexes.

    Then you can just do

    groups = re_fn.match(fileName).groupdict()
    # do stuff with groups['T'], groups['M'], groups['Dt'], groups['H']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a little script to automatically organize some autogenerated spreadsheets. The goal is
im making some script with mechanize.browser module. one of problem is all other thing
Okay, so I am basically making a script to pass post data using cURL.
I'm making an auction site like BidRivals and in my website the auction script
I'm making a code generation script for UN/LOCODE system and the database has unique
I'm making installation script and I'm using ODBC, how can I automatically add a
I am making a shell script that automatically create users, but I cannot think
I'm making a script that handles a predefined set of data, outputting to a
i was making a bash script for my server which pack some directories with
I am making a script that POST some XML to another server but I

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.