# re_digits_extract

 

import re

 

fn_read = r"H:\TEMP\temsys\re_digits_extract.txt"

fn_write = r"H:\TEMP\temsys\re_digits_extract_output.txt"

patt = re.compile(r'<B>\s*(\d+)\s+</B>')

 

'''Example strings:

"Total # Configured: <B> 12345 </B><BR>\n"

"# down: <B> 67890 </B>"

'''

 

f = open(fn_read)

for line in f:

    if line.startswith('Total'):

        i_configured = int(patt.search(line).group(1))

    elif line.startswith('# down:'):

        i_down = int(patt.search(line).group(1))

 

f.close()

 

f = open(fn_write, 'a')

f.write("\nTotal # Configured: %d\nTotal # Down: %d\nTotal # Reporting: %d" % \

        (i_configured, i_down, i_configured-i_down))

f.close()

 

''' Sample Output

Total # Configured: 12345

Total # Down: 67890

Total # Reporting: -55545

'''

 

string1 = "23.45a"

string2 = "23.a"

string3 = ".45a"

patt = re.compile(r'(\d*.\d*)')

 

print float(patt.search(string1).group(1))

print float(patt.search(string2).group(1))

print float(patt.search(string3).group(1))

 

print

patt = re.compile(r'<B>\s*(\d+)\s+</B>')

s1 = "Total # Configured: <B> 12345 </B><BR>\n"

s2 = "# down: <B> 67890 </B>"

print int(patt.search(s1).group(1))

print int(patt.search(s2).group(1))

 

'''

>>> 23.45

23.0

0.45

 

12345

67890

>>>

'''