# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\bStandard Deviation:\h+\K\d[\d.]*"
test_str = (" Format: JPEG (Joint Photographic Experts Group JFIF format)\n"
" Geometry: 934x1189\n"
" Class: DirectClass\n"
" Type: true color\n"
" Depth: 8 bits-per-pixel component\n"
" Channel Depths:\n"
" Red: 8 bits\n"
" Green: 8 bits\n"
" Blue: 8 bits\n"
" Channel Statistics:\n"
" Red:\n"
" Minimum: 3855.00 (0.0588)\n"
" Maximum: 65535.00 (1.0000)\n"
" Mean: 44325.84 (0.6764)\n"
" Standard Deviation: 8571.68 (0.1308)\n"
" Green:\n"
" Minimum: 0.00 (0.0000)\n"
" Maximum: 65021.00 (0.9922)\n"
" Mean: 38079.03 (0.5810)\n"
" Standard Deviation: 8109.29 (0.1237)\n"
" Blue:\n"
" Minimum: 0.00 (0.0000)\n"
" Maximum: 61423.00 (0.9373)\n"
" Mean: 31937.17 (0.4873)\n"
" Standard Deviation: 8422.80 (0.1285)\n"
" Resolution: 72x72 pixels/inch\n"
" Filesize: 101.3Ki\n"
" Interlace: No\n"
" Orientation: Unknown\n"
" Background Color: white\n"
" Border Color: #DFDFDF\n"
" Matte Color: #BDBDBD\n"
" Page geometry: 934x1189+0+0\n"
" Compose: Over\n"
" Dispose: Undefined\n"
" Iterations: 0\n"
" Compression: JPEG\n"
" JPEG-Quality: 70\n"
" JPEG-Colorspace: 2\n"
" JPEG-Colorspace-Name: RGB\n"
" JPEG-Sampling-factors: 2x2,1x1,1x1\n"
" Signature: 68c70dc2aacc49610538d037e7913f66945d7655bb4acc83fa5e13c3581016a9\n"
" Profile-color: 456 bytes\n"
" Tainted: False\n"
" Elapsed Time: 0m:0.006050s\n"
" Pixels Per Second: 175.0Mi")
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