fork download
  1. import re
  2.  
  3. regex = r"([^\s:][^:]*)\s+\:\s+\[\s*([^][]*)\s+]"
  4.  
  5. s = ("Date : [ 2010-01-01 XX:XX:XX ] Age : [ 22 ] Sex : [ M ] : [ XXX ]\n"
  6. "Height(cm) : [ 145 ] Weight(kg) : [ 56.4 ] Race : [ Hispanic ]\n"
  7. "Spirometry : [ restrictive pattern ]\n"
  8. "Treatment response : [ Negative ]\n"
  9. "Tissue volume : [ Normal ]\n"
  10. "Tissue volume\n"
  11. "[ Normal RV ] \n"
  12. "Diffusing capacity : [ Normal capacity ]\n"
  13. "FVC Liters : [ 2.22 ] FVC Liters : [ 67 ] FVC Liters : [ 3.35 ] \n"
  14. "FEV1 Liters : [ 1.96 ] FEV1 Liters : [ 66 ] FEV1 Liters : [ 2.06 ] \n"
  15. "FEV1 / FVC % : [ 58 ] FEV1 / FVC % : [ 62 ]\n"
  16. "DLCO mL/mmHg/min : [ 21.5 ] DLCO mL/mmHg/min : [ 102 ]\n"
  17. "DLCO Adj mL/mmHg/min : [ 21.5 ] DLCO Adj mL/mmHg/min : [ 102 ]\n"
  18. "RV/TLC % : [ 22 ]")
  19.  
  20. print(re.findall(regex, s))
Success #stdin #stdout 0.03s 9592KB
stdin
Standard input is empty
stdout
[('Date', '2010-01-01 XX:XX:XX'), ('Age', '22'), ('Sex', 'M'), ('[ XXX ]\nHeight(cm)', '145'), ('Weight(kg)', '56.4'), ('Race', 'Hispanic'), ('Spirometry', 'restrictive pattern'), ('Treatment response', 'Negative'), ('Tissue volume', 'Normal'), ('Tissue volume\n[ Normal RV ] \nDiffusing capacity', 'Normal capacity'), ('FVC Liters', '2.22'), ('FVC Liters', '67'), ('FVC Liters', '3.35'), ('FEV1 Liters', '1.96'), ('FEV1 Liters', '66'), ('FEV1 Liters', '2.06'), ('FEV1 / FVC %', '58'), ('FEV1 / FVC %', '62'), ('DLCO mL/mmHg/min', '21.5'), ('DLCO mL/mmHg/min', '102'), ('DLCO Adj mL/mmHg/min', '21.5'), ('DLCO Adj mL/mmHg/min', '102'), ('RV/TLC %', '22')]