Applying a regular expression to a text file Python 3
Question: 
    #returns same result i.e. only the first line as many times as 'draws'
    infile = open("results_from_url.txt",'r')
    
    file =infile.read()                                       # essential to get correct formatting
    for line in islice(file, 0, draws):                       # allows you to limit number of draws
        for line in re.split(r"Wins",file)[1].split('\n'):
            mains.append(line[23:38])                         # slices first five numbers from line
            stars.append(line[39:44])                         # slices last two numbers from line
    
    infile.close()
    

I am trying to use the above code to iterate through a list of numbers to
extract the bits of interest. In this attempt to learn how to use regular
expressions in Python 3, I am using lottery results opened from the internet.
All this does is to read one line and return it as many times as I instruct in
the value of 'draws'. Could someone tell me what I have done incorrectly,
please. Does re 'terminate' somehow? The strange thing is if I copy the file
into a string and run this routine, it works. I am at a loss - problem
'reading' a file or in my use of the regular expression?


Answer: I can't tell you why your code doesn't work, because I cannot reproduce the
result you're getting. I'm also not sure what the purpose of

    
    
    for line in islice(file, 0, draws):
    

is, because you never use the `line` variable after that, you immediately
overwrite it with

    
    
    for line in re.split(r"Wins",file)[1].split('\n'):
    

Plus, you could have used `file.split('Wins')` instead of
`re.split(r"Wins",file)`, so you aren't really using regex at all.

* * *

Regex is a tool to find data of a certain format. Why do you use it to split
the input text, when you could use it to find the data you're looking for?

What is it you're looking for? A sequence of seven numbers, separated by
commas. Translated into regex:

    
    
    (?:\d+,){7}
    

However, we want to group the first 5 numbers - the "mains" - and the last 2
numbers - the "stars". So we'll add two named capture groups, named "mains"
and "stars":

    
    
    (?P<mains>(?:\d+,){5})(?P<stars>(?:\d+,){2})
    

This pattern will find all numbers you're looking for.

* * *
    
    
    import re
    
    data= open("infile.txt",'r').read()
    
    mains= []
    stars= []
    
    pattern= r'(?P<mains>(?:\d+,){5})(?P<stars>(?:\d+,){2})'
    iterator= re.finditer(pattern, data)
    for count in range(int(input('Enter number of draws to examine: '))):
        try:
            match= next(iterator)
        except StopIteration:
            print('no more matches')
            break
    
        mains.append(match.group('mains'))
        stars.append(match.group('stars'))
    
    print(mains,stars)
    

This will print something like `['01,03,31,42,46,'] ['04,11,']`. You may want
to remove the commas and convert the numbers to ints, but in essence, this is
how you would use regex.

