# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"SID_W_MAX_TRAN_DT.+?=(?=\s) (?<SID_W_MAX_TRAN_DT>[^\s]+)"
test_str = ("MID_COUNT =========== 20,588,655\n"
" % Total % Received % Xferd Average Speed Time Time Time Current\n"
" Dload Upload Total Spent Left Speed\n"
"Table.+.+?(?=\\.)\\W*(schema.table_name)\\W.+?(?=,+)\\,\\s+numRows\\=+(?<LogTime>[^\\,]+)\n\n"
"Loading data to table schema.table_name\n"
"Table gmr.opcode_tedc_cs_gmr_id_delta stats: [numFiles=5, numRows=8986691, totalSize=937732401, rawDataSize=928745710]\n\n"
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n"
"100 997 100 997 0 0 114k 0 --:--:-- --:--:-- --:--:-- 162k\n\n"
" 0 0 0 397 0 0 1650 0 --:--:-- --:--:-- --:--:-- 1650\n"
"SID_W_MAX_TRAN_DT ========== 9,070,691\n"
" % Total % Received % Xferd Average Speed Time Time Time Current\n"
" Dload Upload Total Spent Left Speed\n\n"
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n"
"100 997 100 997 0 0 108k 0 --:--:-- --:--:-- --:--:-- 139k\n\n"
" 0 0 0 403 0 0 944 0 --:--:-- --:--:-- --:--:-- 944\n"
"NON_PSP_SID_COUNT =========== 39,869,169\n"
" % Total % Received % Xferd Average Speed Time Time Time Current\n"
" Dload Upload Total Spent Left Speed\n\n"
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n"
"100 997 100 997 0 0 112k 0 --:--:-- --:--:-- --:--:-- 162k\n\n"
" 0 0 0 398 0 0 7223 0 --:--:-- --:--:-- --:--:-- 7223\n"
"PSP_SID_COUNT =========== 45,083\n")
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