# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(.+:)\s*\[(.+)\]\s*\n"
test_str = ("Sample Collection Date: [LL4HEMDT]\n"
"White blood cell count:[LL4WBRES]\n"
"Hemoglobin: [LL4HGRES]\n"
"Hematocrit: [LL4HMRES] \n"
"Red blood cell count: [LL4RBRES]\n"
"Platelets: [LL4PLRES]\n\n"
"Neutrophils: [LL4ANRES]\n"
"Lymphocytes: [LL4LCRES]\n"
"Monocytes: [LL4MORES]\n"
"Basophils: [LL4BARES]\n"
"Eosinophils: [LL4ECRES]\n"
"Immature granulocyte: [LL4IGRES]\n"
"Mean corpuscular volume: [LL4MVRES]\n"
"Mean corpuscular hemoglobin: [LL4MHRES]\n"
"Mean corpuscular hemoglobin concentration: [LL4MCRES]\n"
"Red cell distribution width: [LL4RDRES]\n\n\n"
"Sample Collection Date: [LL4CGBDT]\n"
"Prothrombin time: [LL4PTRES]\n"
"Partial thromboplastic time: [LL4PRRES]\n"
"International normalized ratio: [LL4NRRES]\n\n")
subst = "$1 <b> |$2| </b><BR>\\n"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
# 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