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 7767607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:43:10+00:00 2026-06-01T15:43:10+00:00

I have an existing python script that, among other things checks a file against

  • 0

I have an existing python script that, among other things checks a file against a dictionary of regular expressions. If the file contains one of the regular expressions, I note that a dictionary hit has been made, print the line that contains the file and move on. Pretty simple.

However I now want to establihs a different callback function for each RE the file has a hit against. I’m puzzled as to how this can be accomplished in python outside of a long if/elseif block. Is this a case where this should be done in PERL instead? (This means I have to re-write quite a bit of code, hence the reason for not doing so in the first place).

I’ve checked these out (even the links in the posts) for possible work-arounds but have yet to see anything that might work:

Replacements for switch statement in Python?

https://stackoverflow.com/questions/374239/why-doesnt-python-have-a-switch-statement

  • 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-01T15:43:12+00:00Added an answer on June 1, 2026 at 3:43 pm

    You could do something like this :

    def callback1(line, regex_match):
       #do what you want
    
    def callback2(line, regex_match):
       #do what you want... else
    
    
    regex_dict = {
       "first_regex" : callback1,
       "second_regex" : callback2,
    }
    
    file_to_check = open("the_file")
    for line in file_to_check:
        for regex, callback in regex_dict.iteritems():
            result = re.match(regex, line)
            if result:
               callback(line, result)
               break
    

    Then, on each regex match you will call the callback associated with the line and the regex result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that will be doing a lot of things that
I have a large tar.gz file to analyze using a python script. The tar.gz
I have written a python script which read from a txt file and perform
Ubuntu 11.10, Python 2.6. Background: I have an existing Python app that is using
i have a python script, that submits an job to the SGE (Sun Grid
We have an existing java-based heavyweight project that needed an interactive script interpreter. After
I have a small awk script that does some in-place file modifications (to a
I have a python script that calls a USB-based data-acquisition C# dotnet executable. The
I have a python script that calls a USB-based data-acquisition C# dotnet executable. The
I have a python script using imaplib that connects to a gmail account and

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.