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"

One thought on “python regex initial code

  1. nb search is better for multiline regex
    Also add the re.MULTILINE flag toyour compile line
    nb dont forget to ‘|’ OR multiple flags together if you need to

Leave a Reply

Your email address will not be published. Required fields are marked *


two × 9 =