I have this multi-line text, I want to extract the numerical value before the 'Next' text (in this case 13). The numerical values will change, but the location will stay the same, it indicates total # of pages on website. I am having trouble writing the correct regex to return this value:

Previous
1
2
3
...
13
Next

Showing 1 - 100 of 1227 Results[EXTRACT]

pattern =re.compile(r'(\d{1,2})\r\nNext', re.M)
result = pattern.match(text)

The expected return value is 13.

1 Answers

0
user8408080 On Best Solutions
import re

t = """Previous
1
2
3
...
13
Next

Showing 1 - 100 of 1227 Results[EXTRACT]"""

re.search(r"\d+(?=\s+Next)", t).group(0)

    Returns: '13'

The regular expression does a lookahead assertion to see if there is any amount (>1) of digits followed by any amount (>1) of whitespace characters followed by the word Next.