-1

Let's say possible expression values are like this:

a) 10-15 of 25 results
b) 20-25 of 25 results
c) 1,220 - 1,240 of 1300 results
d) 1,280 - 1,300 of 1300 results

I want to test the expression such that b and d would return true and a and c would return false. In other words, I'm looking for the end of sequence condition.

Lawrence DeSouza
  • 984
  • 5
  • 16
  • 34

3 Answers3

1

This should do the trick:

end_re = re.compile(r'([\d,]+) of \1 results')
def is_end(s):
   return bool(end_re.search(s))

the idea is to find the same number in the last portion of the results. See this answer for more info.

EDIT: fixed the issue with the comma mentioned by asker

Community
  • 1
  • 1
Pykler
  • 14,565
  • 9
  • 41
  • 50
  • I like the simplicity, but it is failing because of the comma in the numbers. i.e. mystring = "234 - 1,250 of 1,250 results" returns false. but it does work where there is no comma in the number – Lawrence DeSouza Feb 06 '14 at 17:22
  • fixed it, thanks for the followup ... your answer should also work, the only minor concern is the whole digits group in your answer is optional so your regex might match nothing in the group. However regexs are greedy by default so if your input is exactly how you describe always then your answer would work. – Pykler Feb 07 '14 at 19:01
1

Thanks to Pykler for pointing me in the right direction. The answer had to account for the comma, so I have accommodated the commas with the following modification to his answer:

def is_end( s ) :  
    end_re = re.compile(r'([\d]*,?[\d]*) of \1 results')    
    return bool(end_re.search(s))
Lawrence DeSouza
  • 984
  • 5
  • 16
  • 34
0

This works -

input = '''
a) 10-15 of 25 results
b) 20-25 of 25 results
c) 1,220 - 1,240 of 1300 results
d) 1,280 - 1,300 of 1300 results
'''

import re
regex = r'([0-9\,]+)[ ]*\-[ ]*([0-9\,]+)[ ]+of[ ]+([0-9]+)'
for ip in input.split("\n"):
    matches = re.findall(regex, ip)
    if matches and len(matches[0]) == 3:
        r = int(matches[0][1].replace(",",""))
        results = int(matches[0][2])
        if(r == results):
            print "true"
        else:
            print "false"

'''OUTPUT:
false
true
false
true
'''
Kamehameha
  • 5,423
  • 1
  • 23
  • 28