What can be the Regular Expression for ASTM Standard protocol String?
"P|1||123456||^|||U" + ProtocolASCII.LF
+ "O|1||138||||||||||||O" + ProtocolASCII.LF
+ "R|1|^^^BE(B)||mmol/L||C||||||20150819144937" + ProtocolASCII.LF
+ "R|2|^^^BEecf||mmol/L||C" + ProtocolASCII.LF
+ "R|3|^^^Ca++|1.17|mmol/L" + ProtocolASCII.LF
Where ProtocolASCII.LF = '\n'. I am writing String parser to extract data from this String.
I have splited String based on \n and now I need to parse each String to extract data.
Is there any Regular Expression so that I can map to and get desired result?
The String P|1||123456||^|||U" + ProtocolASCII.LF present patient number where P means patient tag or patient information and 123456 is patient number.
For String R|3|^^^Ca++|1.17|mmol/L" + ProtocolASCII.LF is the result of laboratory test performed. Where R indicated result, ^^^Ca++ indicates name of the result, 1.17 is result value and mmol/L is unit.
Currently I am parsing String like:
String[] resultArray = dataString.split("[\\r\\n]+");
HashMap<String, Object> resultData = new HashMap<>();
List<Result> sampleResults = new ArrayList<>();
for (String res : resultArray) {
//Get first character of String
char startChar = res.charAt(0);
switch (startChar) {
case ProtocolASCII.STX:
//Handle Header information. This is special case
if (res.charAt(2) == ProtocolASCII.Alphabet.H
|| res.charAt(1) == ProtocolASCII.Alphabet.H) {
resultData.put(Key.MACHINE_INFO, getHeaderInfo(res));
//System.out.println(res.charAt(2));
}
break;
case ProtocolASCII.Alphabet.P:
resultData.put(Key.PATIENT, getPatientInfo(res));
break;
case ProtocolASCII.Alphabet.O:
resultData.put(Key.ORDER, getOrderInfo(res));
break;
case ProtocolASCII.Alphabet.R:
sampleResults.add(getResultInfo(res));
break;
case ProtocolASCII.Alphabet.L:
// TODO - Handle end of line
break;
}
}
This is my complete ASTM Protocol String:
/**
* Sample string
*/
public static final String MACHINE_STRING_ASTM = ProtocolASCII.STX + "1H|\\^&|||GEM 3000^5.6.1 ^21152^^023665^2.4|||||||||20150819154754" + ProtocolASCII.LF
+ "P|1||322061||^|||U" + ProtocolASCII.LF
+ "O|1||138||||||||||||O" + ProtocolASCII.LF
+ "R|1|^^^BE(B)||mmol/L||C||||||20150819144937" + ProtocolASCII.LF
+ "R|2|^^^BEecf||mmol/L||C" + ProtocolASCII.LF
+ "R|3|^^^Ca++|1.17|mmol/L" + ProtocolASCII.LF
+ "R|4|^^^Ca++(7.4)||mmol/L||C" + ProtocolASCII.LF
+ "R|5|^^^HCO23-||mmol/L||C" + ProtocolASCII.LF
+ "R|6|^^^HCO3std||mmol/L||C" + ProtocolASCII.LF
+ "R|7|^^^K+|5.0|mmol/L" + ProtocolASCII.LF
+ "R|8|^^^Na+|140|mmol/L" + ProtocolASCII.LF
+ "R|9|^^^SO2c||%||C" + ProtocolASCII.LF
+ "R|10|^^^TCO2||mmol/L||C" + ProtocolASCII.LF
+ "R|11|^^^THbc||g/dL||C" + ProtocolASCII.LF
+ "R|12|^^^Temp|37.0|C" + ProtocolASCII.LF
+ "L|1" + ProtocolASCII.EOT;
Is there a way I can extract data using Regular Expressions?
Let me know if further information is required.
Thanks
You can try this for R and P:
(?:^(R)\|(\d+)\|\^*([^|]*)\|([^|]*)\|(?:([^|]*)\|)?.*$)|(?:^(P)\|(\d+)\|\|(\d+).*$)
Explanation
Try this:
final String regex = "(?:^(R)\\|(\\d+)\\|\\^*([^|]*)\\|([^|]*)\\|(?:([^|]*)\\|)?.*$)|(?:^(P)\\|(\\d+)\\|\\|(\\d+).*$)";
final String string = "P|1||322061||^|||U \n"
+ "O|1||138||||||||||||O \n"
+ "R|1|^^^BE(B)||mmol/L||C||||||20150819144937 \n"
+ "R|2|^^^BEecf||mmol/L||C \n"
+ "R|3|^^^Ca++|1.17|mmol/L \n"
+ "R|4|^^^Ca++(7.4)||mmol/L||C \n"
+ "R|5|^^^HCO23-||mmol/L||C \n"
+ "R|6|^^^HCO3std||mmol/L||C \n"
+ "R|7|^^^K+|5.0|mmol/L \n"
+ "R|8|^^^Na+|140|mmol/L \n"
+ "R|9|^^^SO2c||%||C \n"
+ "R|10|^^^TCO2||mmol/L||C \n"
+ "R|11|^^^THbc||g/dL||C \n"
+ "R|12|^^^Temp|37.0|C\n";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
//System.out.println("Full match: " + matcher.group(0));
if(matcher.group(1)!=null && matcher.group(1).equalsIgnoreCase("R"))
{
System.out.println("Result NO:"+matcher.group(2));
System.out.println("Component:"+matcher.group(3));
System.out.println("value:"+matcher.group(4));
System.out.println("unit:"+matcher.group(5));
System.out.println("##############################");
}
else if(matcher.group(6)!=null && matcher.group(6).equalsIgnoreCase("P"))
{
System.out.println("Patient :"+matcher.group(7));
System.out.println("Patient Number:"+matcher.group(8));
System.out.println("##############################");
}
}
Sample Output:
Patient :1
Patient Number:322061
##############################
Result NO:1
Component:BE(B)
value:
unit:mmol/L
##############################
Result NO:2
Component:BEecf
value:
unit:mmol/L
##############################
Result NO:3
Component:Ca++
value:1.17
unit:mmol/L
R
##############################
Result NO:5
Component:HCO23-
value:
unit:mmol/L
##############################
Result NO:6
Component:HCO3std
value:
unit:mmol/L
##############################
Result NO:7
Component:K+
value:5.0
unit:mmol/L
R
##############################
Result NO:9
Component:SO2c
value:
unit:%
##############################
Result NO:10
Component:TCO2
value:
unit:mmol/L
##############################
Result NO:11
Component:THbc
value:
unit:g/dL
##############################
Result NO:12
Component:Temp
value:37.0
unit:null
##############################
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With