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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:51:34+00:00 2026-05-26T18:51:34+00:00

there are lots of questions about split in python, but I couldn’t find corresponding

  • 0

there are lots of questions about split in python, but I couldn’t find
corresponding to my problem. I want to split a string, but need to have
different values for the splitter, depending on a condition. For the test
case, my string is “11xx22xx33xxBEGINxx44xx55xxENDxx66xx77”. I want to
process this string in chunks, meaning I want to step through it like
this:

split off ’11’, do something with it

split off ’22’, do something with it

split off ’33’, do something with it

split off ‘BEGINxx44xx55xxEND’, do something with it

split off ’66’, do something with it

split off ’77’, do something with it

I tried a recursive function:

import string

mystring = "11xx22xx33xxBEGINxx44xx55xxENDxx66xx77"

def makechunks(s):
    try: splitter
    except NameError:
        splitter = "xx"
    whole = s.split(splitter, 1)
    current = whole[0]
    try: whole[1]
    except NameError:
        return
    else:
        rest = whole[1]
        if current.find("BEGIN", 0, 5):
            splitter = "END"
        else:
            splitter = "xx"
        makechunks(rest)
        print("AA", current, "BB")

makechunks(mystring)

But I’m getting the error “list index out of range.” Maybe my entire
approach is flawed, and there are better ways to achieve what I want? I’ll
be grateful for any hint.

Thanks!

  • 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-26T18:51:35+00:00Added an answer on May 26, 2026 at 6:51 pm

    You can do it with a regular expression:

    matches = re.findall('(?:^|xx)(BEGIN.*?END|.*?)(?=xx|$)', s)
    

    Result:

    ['11', '22', '33', 'BEGINxx44xx55xxEND', '66', '77']
    

    See it working online: ideone

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

Sidebar

Related Questions

There are already lots of questions on SO about exceptions, but I can't find
There's lots of questions about installers but I haven't seen one about whether or
There are lots of questions about recovering passwords on SO, but not about changing
There are lots of questions about this but I've not been able to solve
I know there are lots of questions about background images but I'm looking for
I know there are lots of questions with this title but I could find
There are lots of questions about how to force the browser to cache or
There are lots of questions on how to strip html tags, but not many
There are lots of non-image-based CAPTCHA ideas floating around. But what about the old-fashioned
There are lots of questions on SO asking about the pros and cons of

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.