首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASTM协议字符串的正则表达式

ASTM协议字符串的正则表达式
EN

Stack Overflow用户
提问于 2017-01-19 07:43:18
回答 1查看 1.6K关注 0票数 3

ASTM标准协议字符串的正则表达式可以是什么?

代码语言:javascript
复制
"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

在哪里ProtocolASCII.LF = '\n'。我正在编写字符串解析器来从这个字符串中提取数据。

我已经基于\n拆分了字符串,现在我需要解析每个字符串来提取数据。

是否有任何正则表达式使我能够映射到并获得所需的结果?

字符串P|1||123456||^|||U" + ProtocolASCII.LF表示患者编号,其中P表示患者标记或患者信息,123456表示患者编号。

对于字符串,R|3|^^^Ca++|1.17|mmol/L" + ProtocolASCII.LF是实验室测试的结果。其中R表示结果,^^^Ca++表示结果名称,1.17表示结果值,mmol/L表示单位。

目前,我正在解析字符串,如:

代码语言:javascript
复制
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;
        }
    }

这是我完整的ASTM协议字符串:

代码语言:javascript
复制
/**
 * 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;

有没有方法可以使用正则表达式提取数据?

如果需要进一步的资料,请告诉我。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-19 09:20:00

您可以为R和P尝试此方法:

代码语言:javascript
复制
(?:^(R)\|(\d+)\|\^*([^|]*)\|([^|]*)\|(?:([^|]*)\|)?.*$)|(?:^(P)\|(\d+)\|\|(\d+).*$)

解释

试试这个:

代码语言:javascript
复制
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("##############################");
    }
}

样本输出:

代码语言:javascript
复制
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
##############################
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41736359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档