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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:11:00+00:00 2026-05-11T16:11:00+00:00

I have a somewhat complex regular expression which I’m trying to match against a

  • 0

I have a somewhat complex regular expression which I’m trying to match against a long string (65,535 characters). I’m looking for multiple occurrences of the re in the string, and so am using finditer. It works, but for some reason it hangs after identifying the first few occurrences. Does anyone know why this might be? Here’s the code snippet:

pattern = "(([ef]|([gh]d*(ad*[gh]d)*b))d*b([ef]d*b|d*)*c)"

matches = re.finditer(pattern, string)
for match in matches:
    print "(%d-%d): %s" % (match.start(), match.end(), match.group())

It prints out the first four occurrences, but then it hangs. When I kill it using Ctrl-C, it tells me it was killed in the iterator:

Traceback (most recent call last):
  File "code.py", line 133, in <module>
    main(sys.argv[1:])
  File "code.py", line 106, in main
    for match in matches:
KeyboardInterrupt

If I try it with a simpler re, it works fine.

I’m running this on python 2.5.4 running on Cygwin on Windows XP.

I managed to get it to hang with a very much shorter string. With this 50 character string, it never returned after about 5 minutes:

ddddddeddbedddbddddddddddddddddddddddddddddddddddd

With this 39 character string it took about 15 seconds to return (and display no matches):

ddddddeddbedddbdddddddddddddddddddddddd

And with this string it returns instantly:

ddddddeddbedddbdddddddddddddd
  • 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-11T16:11:00+00:00Added an answer on May 11, 2026 at 4:11 pm

    Definitely exponential behaviour. You’ve got so many d* parts to your regexp that it’ll be backtracking like crazy when it gets to the long string of d’s, but fails to match something earlier. You need to rethink the regexp, so it has less possible paths to try.

    In particular I think:

    ([ef]d\*b|d\*)*</pre></code> and <code><pre>([ef]|([gh]d\*(ad\*[gh]d)\*b))d\*b
    

    Might need rethinking, as they’ll force a retry of the alternate match. Plus they also overlap in terms of what they match. They’d both match edb for example, but if one fails and tries to backtrack the other part will probably have the same behaviour.

    So in short try not to use the | if you can and try to make sure the patterns don’t overlap where possible.

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

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm pretty sure that this is not possible. Here's why:… May 11, 2026 at 5:44 pm
  • Editorial Team
    Editorial Team added an answer Biggest reason is that it violates the single responsiblity principle.… May 11, 2026 at 5:44 pm
  • Editorial Team
    Editorial Team added an answer Try this rule: RewriteCond %{THE_REQUEST} ^GET\ /script\$([^\s]+) RewriteRule ^script\$.+ anotherscript?ip=%{REMOTE_ADDR}&%1… May 11, 2026 at 5:44 pm

Related Questions

I have an implicit scalar field defined in 2D, for every point in 2D
I'm trying to convert an XML file into the markup used by dokuwiki, using
I'm trying to create Excel 2007 Documents programmatically. Now, there are two ways I've
I've heard from a variety of places that global variables are inherently nasty and
I'm getting in to a situation where I have several interacting widgets (on a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.