Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Regex finding specific number in a file

I am looking for numbers which occur in specific place in large files. Here is sample of the file containing the expression I'm looking for:

<span style="margin-left: 3px;" class="label">Number of results: </span><span class="text">10</span>

And here is my code:

import re

with open("search.txt") as file:    
    for line in file:
        query = re.search('(?<=Number of results: </span><span class="text">)(\d+)', line)
        if query:
            print(query.group())

Expected output (in this case) is 10. My main problem is that when I run the commands in the python terminal it works, regex works in online regex tester, even my text editor finds the number by the expression, but when I run the code as a script it returns nothing or None without the ".group()" in the last line.

If I run the script on the single line passed as a string to the regex it also returns correct value, but when used on a file it fails.

Any ideas or hints? Thanks in advance.

Comments