# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\d+-\d[\d.]*\s*\K\S+"
test_str = ("WBC 4.27-11.40 k/uL 3.64 (L)\n"
"RBC 3.90-5.03 m/uL 4.30\n"
"Hemoglobin 10.6-13.4 g/dL 13.0\n"
"Hematocrit 32.2-39.8 % 36.1\n"
"MCV 74.4-87.6 fL 84.0\n"
"MCH 24.8-29.5 pG 30.2 (H)\n"
"MCHC 31.8-34.9 g/dL 36.0 (H)\n"
"RDW-CV 12.2-14.4 % 13.2\n"
"Platelet Count 150-400 k/uL 175\n"
"MPV 9.2-11.4 fL 8.6 (L)\n"
"Neut% 28.6-74.5 % 43.1\n"
"Abs Neut (ANC) 1.63-7.87 k/uL 1.57 (L)\n"
"Lymph% 15.5-57.8 % 43.7\n"
"Abs Lymph 0.97-4.28 k/uL 1.59\n"
"Mono% 4.2-12.3 % 9.3\n"
"Abs Mono 0.19-0.85 k/uL 0.34\n"
"Eosin% 0.0-4.7 % 3.6\n"
"Abs Eosin 0.00-0.52 k/uL 0.13\n"
"Baso% 0.0-0.7 % 0.3\n"
"Abs Baso 0.00-0.06 k/uL 0.01")
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