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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:13:06+00:00 2026-05-21T02:13:06+00:00

So I get some input in python that I need to parse using regexps.

  • 0

So I get some input in python that I need to parse using regexps.

At the moment I’m using something like this:

matchOK = re.compile(r'^OK\s+(\w+)\s+(\w+)$')
matchFailed = re.compile(r'^FAILED\s(\w+)$')
#.... a bunch more regexps

for l in big_input:
  match = matchOK.search(l)
  if match:
     #do something with match
     continue
  match = matchFailed.search(l)
  if match:
     #do something with match
     continue
  #.... a bunch more of these 
  # Then some error handling if nothing matches

Now usually I love python because its nice and succinct. But this feels verbose. I’d expect to be able to do something like this:

for l in big_input:      
  if match = matchOK.search(l):
     #do something with match     
  elif match = matchFailed.search(l):
     #do something with match 
  #.... a bunch more of these
  else
    # error handling

Am I missing something, or is the first form as neat as I’m going to get?

  • 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-05-21T02:13:07+00:00Added an answer on May 21, 2026 at 2:13 am
    class helper:
        def __call__(self, match):
            self.match= match
            return bool(match)
    
    h= helper()
    for l in big_input:      
        if h(matchOK.search(l)):
            # do something with h.match     
        elif h(matchFailed.search(l)):
            # do something with h.match 
        ... # a bunch more of these
        else:
            # error handling
    

    Or matchers as class methods:

    class matcher:
        def __init__(self):
            # compile matchers
            self.ok= ...
            self.failed= ...
            self....= ...
    
        def matchOK(self, l):
            self.match= self.ok(l)
            return bool(self.match)
    
        def matchFailed(self, l):
            self.match= self.failed(l)
            return bool(self.match)
    
        def match...(self, l):
            ...
    
    m= matcher()
    for l in big_input:      
        if m.matchOK(l):
            # do something with m.match     
        elif m.matchFailed(l):
            # do something with m.match 
        ... # a bunch more of these
        else:
            # error handling
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here and I wanted to get some input from
I get asked this question a lot and I thought I'd solicit some input
I need to get some information that is contained in the MFT on a
I am trying to get some accurate runtime comparisons of PHP vs Python (and
I need to get some AVI animations for use with the Borland VCL TAnimate
I'm trying to get some code working that a previous developer has written. Yep,
Could I get some confirmations from the community that I'm not going mad, and
I'm building a game server in Python and I just wanted to get some
A python script I need to run takes input only from a file passed
To get some smooth graphics, I want to draw oversampled by factor 2 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.