Monthly Archives: April 2014

python regex initial code

I love the power of regex but boy is it a pain to use. I like to use this code as a start and work it into the code i need exactly. Basically I’ll get my regex string correct using an online regex engine such as http://www.regexr.com/ then i bung this code into my program then I feed it the lines of text in turn.

import re
RES_STRING='ID=[0-9]{8}'
REGEX_HAYSTACK='ID=50933999'

REGEX_STRING=re.compile(RES_STRING)
print "searching for "+RES_STRING+" in "+REGEX_HAYSTACK
REGEX_MATCH = REGEX_STRING.match(REGEX_HAYSTACK)
if REGEX_MATCH:
print "found '"+REGEX_MATCH.group()+"'"
else:
print "No match found"