# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"Tapper\s*(\d*(?:\.\d+)?)[\s\S]*?M845"
test_str = ("%\n"
"N1 ( POSTED FILE NAME - WORKNC POST )\n"
"N2 ( INPUT FILE NAME - _6033_Cavity__01.TBA)\n"
"N3 (DATE/TIME: Thu Mar 15 06:36:08 2018)\n"
"N4 G0 G40 G80 G90 G98\n"
"N5 G17\n"
"N6 G57H901\n"
"N7 G173W0.0\n"
"N8 B0.000\n"
"N9 (Tapper 0.250000)\n"
"N10 T21\n"
"N11 M06\n"
"N12 S100\n"
"N13 M843\n"
"N14 G173 W0.0\n"
"N15 (- )\n"
"N16 ( Tapping )\n"
"N17 G0 G90 X-0.0001 Y8.8135\n"
"N18 G43 Z10.0632 H21\n"
"N19 G01 F500. X-0.0001 Y8.8135\n"
"N20 G01 Z9.7163 F500.\n"
"N21 G98 G84 X-0.0001 Y8.8135 Z6.0376 R7.6376 E10.\n"
"N22 G80 G01 F500.\n"
"N23 X-0.0001 Y8.8135 Z9.7163\n"
"N24 X-0.0001 Y8.8135 Z9.7163\n"
"N25\n"
"N26 M845\n"
"N27 G91 G28 Z0\n"
"N28 G90\n"
"N29 G57H901\n"
"N30 G173W0.0\n"
"N31 B0.000\n"
"N32 (Drill 0.005000)\n"
"N33 T19\n"
"N34 M06\n"
"N35 S5000\n"
"N36 M3\n"
"N37 G173 W0.0\n"
"N38 (- )\n"
"N39 ( Contour Chamfer )\n"
"N40 G0 G90 X-0.0001 Y8.8135\n"
"N41 G43 Z9.7163 H19\n"
"N42 Z7.6375\n"
"N43 G01 Z9.8376 F500.\n"
"N44 X-0.0001 Y8.8135 Z10.0632\n"
"N45\n"
"N46 M05\n"
"N47 G91 G28 Z0\n"
"N48 G90\n"
"N49 G57H901\n"
"N50 G173W0.0\n"
"N51 B0.000\n"
"N52 (Tapper 0.750000)\n"
"N53 T21\n"
"N54 M06\n"
"N55 S100\n"
"N56 M843\n"
"N57 G173 W0.0\n"
"N58 (- )\n"
"N59 ( Tapping )\n"
"N60 G0 G90 X-0.0001 Y8.8135\n"
"N61 G43 Z10.0632 H21\n"
"N62 G01 F500. X-0.0001 Y8.8135\n"
"N63 G01 Z9.7163 F500.\n"
"N64 G98 G84 X-0.0001 Y8.8135 Z6.0376 R7.6376 E10.\n"
"N65 G98 G84 X-0.0001 Y10.8135 Z6.0376 R7.6376 E10.\n"
"N66 G80 G01 F500.\n"
"N67 X-0.0001 Y8.8135 Z9.7163\n"
"N68 X-0.0001 Y8.8135 Z9.7163\n"
"N69\n"
"N70 M845\n"
"N71 G91 G28 Z0\n"
"N72 G90\n"
"N73 M30\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