# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(.+\s-\s\w{2})$|^(Total\s\d{1,5}:.+)$"
test_str = ("Total\n"
"2015\n"
"DOLLAR\n"
"AMOUNT\n"
"Oct-15 DIFF 15-16\n"
"Total\n"
"2015\n"
"COMPANY 1 - WI\n"
"Nuts $ 59.85 $ 0.00 $ 135.45 $ 0.00 $ 135.45 $0.00\n"
"Bolts $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
"Screws $ 449.20 $ 0.00 $ 541.23 $ 0.00 $ 541.23 $0.00\n"
"Total 7765: $ 509.05 $ 0.00 $ 676.68 $ 0.00 $ 676.68 $0.00\n"
"Company 2 - NE\n"
"Nuts $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
"Bolts $ 0.00 $ 55.67 $ 542.48 $ 719.82 $(177.34) $777.02\n"
"Total 1876: $ 0.00 $ 55.67 $ 2,960.52 $ 4,265.82 $(1,305.30) $5,854.60\n"
"Company 3 - MN\n"
"Nuts $ 109.52 $ 606.52 $ 858.36 $ 606.52 $ 251.84 $606.52\n"
"Paper $ 0.00 $ 0.00 $ 483.82 $ 0.00 $ 483.82 $678.30\n"
"Gas $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
"OTHER $ 521.49 $ 0.00 $ 521.49 $ 0.00 $ 521.49 $0.00\n"
"Total 6524: $ 631.01 $ 606.52 $ 3,909.09 $ 606.52 $ 3,302.57 $1,284.82\n"
"Company 4 - IA\n"
"Anything $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
"Other $ 0.00 $ 0.00 $ 171.90 $ 0.00 $ 171.90 $0.00\n"
"Total 1123: $ 0.00 $ 0.00 $ 171.90 $ 0.00 $ 171.90 $0.00")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html