UP | HOME

Simple Sample of Regular Expression in Python

import re

def read_file():\\
file_name = 'merge.log'\\
file = open(file_name, 'r')\\
file_content = file.read()\\
patterns =
re.compile(r'(r\d{5}\s\|\scn-cma)(.*(?!cn-cma).*)(r\d{5}\s\|\s(?!cn-cma))',
re.DOTALL)\\
results = patterns.search(file_content)\\
#results = patterns.match(file_content)\\
print dir(results)\\
if results :\\
results = results.groups()\\
print results[0]\\
def display(x):\\
print'$' * 50, '\n', x\\
#[ display(x) for x in results ]\\
file.close()