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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:07:42+00:00 2026-05-30T04:07:42+00:00

How can I obtain the number of overlapping regex matches using Python? I’ve read

  • 0

How can I obtain the number of overlapping regex matches using Python?

I’ve read and tried the suggestions from this, that and a few other questions, but found none that would work for my scenario. Here it is:

  • input example string: akka
  • search pattern: a.*k

A proper function should yield 2 as the number of matches, since there are two possible end positions (k letters).

The pattern might also be more complicated, for example a.*k.*a should also be matched twice in akka (since there are two k‘s in the middle).

  • 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-30T04:07:43+00:00Added an answer on May 30, 2026 at 4:07 am

    Yes, it is ugly and unoptimized but it seems to be working. This is a simple try of all possible but unique variants

    def myregex(pattern,text,dir=0):
        import re
        m = re.search(pattern, text)
        if m:
            yield m.group(0)
            if len(m.group('suffix')):
                for r in myregex(pattern, "%s%s%s" % (m.group('prefix'),m.group('suffix')[1:],m.group('end')),1):
                    yield r
                if dir<1 :
                    for r in myregex(pattern, "%s%s%s" % (m.group('prefix'),m.group('suffix')[:-1],m.group('end')),-1):
                        yield r
    
    
    def myprocess(pattern, text):    
        parts = pattern.split("*")    
        for i in range(0, len(parts)-1 ):
            res=""
            for j in range(0, len(parts) ):
                if j==0:
                    res+="(?P<prefix>"
                if j==i:
                    res+=")(?P<suffix>"
                res+=parts[j]
                if j==i+1:
                    res+=")(?P<end>"
                if j<len(parts)-1:
                    if j==i:
                        res+=".*"
                    else:
                        res+=".*?"
                else:
                    res+=")"
            for r in myregex(res,text):
                yield r
    
    def mycount(pattern, text):
        return set(myprocess(pattern, text))
    

    test:

    >>> mycount('a*b*c','abc')
    set(['abc'])
    >>> mycount('a*k','akka')
    set(['akk', 'ak'])
    >>> mycount('b*o','bboo')
    set(['bbo', 'bboo', 'bo', 'boo'])
    >>> mycount('b*o','bb123oo')
    set(['b123o', 'bb123oo', 'bb123o', 'b123oo'])
    >>> mycount('b*o','ffbfbfffofoff')
    set(['bfbfffofo', 'bfbfffo', 'bfffofo', 'bfffo'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can obtain the phone number from an incoming call or from a sms
How can I obtain the command line arguments of another process? Using static functions
How can I obtain the physical machine name that my jvm is running in?
How can I just obtain the string value of a Xml node using xpath
I have this function that creates a unique number for hard-disk and CPU combination.
Can someone explain the solution of this problem to me? Suppose that you are
I am using EWS and wish to obtain the conversation history folder from Office365
I am already using this example of how to read large data files in
How can i read an AudioInputStream upto a particular number of bytes/microsecond position ?
I come from ANT where you can invoke a .java file to obtain the

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.