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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:23:25+00:00 2026-06-16T04:23:25+00:00

I am looking for repeating patterns inside an HTML page. The patterns I am

  • 0

I am looking for repeating patterns inside an HTML page.
The patterns I am interested in start after the prefix “<h2>Seasons</h2>”
The same patterns occur before the prefix too, I am not interested in those.

I tried (and failed) with the following python code (I simplified the pattern to ‘<a href=.+?</a>’ for the sake of making this question readable):

matches = re.compile('<h2>Seasons</h2>.+?(<a href=.+?</a>)+',re.DOTALL).findall(page)  
for ref in matches  
   print ref

Given the page:

blah blah html stuff 
<h2>Seasons</h2>  
blah blah  more html stuff
<a href=http://www.111.com>111</a><a href=http://www.222.com>222</a><a href=http://www.333.com>333</a>

The output is

<a href=http://www.333.com>333</a>  

So it only prints the last match, the other two do not make it to the findall list.
How do I do to iterate over all matches of the groups?

  • 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-16T04:23:26+00:00Added an answer on June 16, 2026 at 4:23 am

    The problem is that the regex matches only a single time. The parenthesized group matches multiple times, but the regex as a whole only matches once. This means only one match is returned, the last one.

    To get around this you need to write a regex that matches multiple times. You might think to use a lookbehind assertion for the <h2> element like so:

    (?<=<h2>Seasons</h2>.+?)(<a href=.+?</a>)    # doesn't work
    

    This says to find <a> elements, but only if they’re preceded by <h2>Seasons</h2>. Unfortunately lookbehind strings have to be of fixed length. You can’t put .+? in a lookbehind assertion. So that approach is out.

    Next up is to find the location of the <h2> element first, then perform the regex search starting from there.

    >>> re.findall('<a href=.+?</a>', page[page.find('<h2>Seasons</h2>'):], re.DOTALL)
    ['<a href=http://www.111.com>111</a>', '<a href=http://www.222.com>222</a>', '<a href=http://www.333.com>333</a>']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to start a IntentService at the start of every hour repeating exactly
I'm looking for a repeating timer object that allows me to create it once
I'm finding myself repeating the same snippets of code again and again, is it
I'm looking for a way to generate pdf files from html In order to
I am looking for an idea of a clean generic way to describe repeating
I'm looking for a little sage advice from some helpful HTML/CSS masters. I am
Sorry about repeating the Question, but this is some what different. I'm looking for
I'm looking for a way to clean strings from their longest repeating pattern. I
I have a repeating horizontal pattern in a background layer in HTML. I could
I am looking for an efficient way to extract the shortest repeating substring. For

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.