Using python regex to find repeated values after a header
Question: If I have a string that looks something like:

    
    
    s = """
    ...
    Random Stuff
    ...
    HEADER
    a 1
    a 3
    # random amount of rows
    a 17
    RANDOM_NEW_HEADER
    a 200
    a 300
    ...
    More random stuff
    ...
    """
    

Is there a clean way to use regex (in Python) to find all instances of `a \d*`
after HEADER, but before the pattern is broken by SOMETHING_TOTALLY_DIFFERENT?
I thought about something like:

    
    
    import re
    pattern = r'HEADER(?:\na \d*)*\na (\d*)'
    
    print re.findall(pattern, s)
    

Unfortunately, regex doesn't find overlapping matches. If there's no sensible
way to do this with regex, I'm okay with anything faster than writing my own
for loop to extract this data.

(TL;DR -- There's a distinct header, followed by a pattern that repeats. I
want to catch each instance of that pattern, as long as there isn't a break in
the repetition.)

EDIT:

To clarify, I don't necessarily know what SOMETHING_TOTALLY_DIFFERENT will be,
only that it won't match `a \d+`. I want to collect all consecutive instances
of `\na \d+` that follow `HEADER\n`.


Answer: How about a simple loop?

    
    
    import re
    
    e = re.compile(r'(a\s+\d+)')
    
    header = 'whatever your header field is'
    breaker = 'something_different'
    
    breaker_reached = False
    header_reached = False
    
    results = []
    
    with open('yourfile.txt') as f:
        for line in f:
            if line == header:
                # skip processing lines unless we reach the header
                header_reached = True
                continue
            if header_reached:
                i = e.match(line)
                if i and not breaker_reached:
                   results.append(i.groups()[0])
                else:
                   # There was no match, check if we reached the breaker
                   if line == breaker:
                       breaker_reached = True
    

