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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:31:52+00:00 2026-06-04T06:31:52+00:00

I get deep indentation when I write code like below match = re.search(some_regex_1, s)

  • 0

I get deep indentation when I write code like below

match = re.search(some_regex_1, s)
if match:
    # do something with match data
else:
    match = re.search(some_regex_2, s)
    if match:
        # do something with match data
    else:
        match = re.search(soem_regex_3, s)
        if match:
            # do something with match data
        else:
            # ...
            # and so on

I tried to rewrite as:

if match = re.search(some_regex_1, s):
    # ...
elif match = re.search(some_regex_2, s):
    # ...
elif ....
    # ...
...

but Python doesn’t allow that syntax. What should I do to avoid deep indentation in this case?

  • 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-04T06:31:54+00:00Added an answer on June 4, 2026 at 6:31 am
    regexes = (regex1, regex2, regex3)
    for regex in regexes:
        match = re.search(regex, s)
        if match:
            #do stuff
            break
    

    Alternatively (more advanced):

    def process1(match_obj):
        #handle match 1
    
    def process2(match_obj):
        #handle match 2
    
    def process3(match_obj):
        #handle match 3
    .
    .
    .
    handler_map = ((regex1, process1), (regex2, process2), (regex3, process3))
    for regex, handler in handler_map:
        match = re.search(regex, s)
        if match:
            result = handler(match)
            break
    else:
        #else condition if no regex matches
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the memberOf property for my user using this code: DirectorySearcher search =
I have a simple one level deep vocabulary taxonomy. Something like Vocabulary->Term->Node. What I
how do i get the array value from below code?. and use while loop
I'm way in deep water here trying to get ahold of a vc. My
How can I get more information from an error like this? => Booting WEBrick
I am currently taking a c++ course and trying to get a deep understanding
I am trying to get 4 List deep List collection, List<List<List<List<int>>>> . From my
Is it possible to get Core Data cascade delete to delete objects more than
Is there a way to deep clone JSoup Document object and get back exactly
When I get exceptions, it is often from deep within the call stack. When

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.