Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ASTM协议字符串的正则表达式_Java_Regex_String_Pattern Matching - Fatal编程技术网

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

Java ASTM协议字符串的正则表达式,java,regex,string,pattern-matching,Java,Regex,String,Pattern Matching,ASTM标准协议字符串的正则表达式可以是什么 "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

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

"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 | | 124456 | | | | | | U“+协议ASCII.LF
表示患者编号,其中p表示患者标签或患者信息,123456表示患者编号

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

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

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;
        }
    }
是否有一种方法可以使用提取数据

如果需要更多信息,请告诉我


谢谢

您可以为R和p尝试以下功能:

(?:^(R)\|(\d+)\|\^*([^|]*)\|([^|]*)\|(?:([^|]*)\|)?.*$)|(?:^(P)\|(\d+)\|\|(\d+).*$)

试试这个:

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("##############################");
    }
}
样本输出:

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
##############################

给我们一个清晰的画面,当它分别以P,R,O,L开头时,你想从这些行中得到什么。。。然后可能不必理解ASTM协议,但给你一个解决方案。。。比方说,如果这行以P开头,那么对于这行的其余部分,您想解析什么,对于R,O,也一样,L@Maverick_Mrt我也提到过这个问题!如果它以R开头,我们需要得到除|之外的值,因为它是分隔符。例如,R | 3 |是化学成分Ca++的测试结果,其值为1.17,单位为mmol/L。因此,每当R | n |出现时,我都需要提取此数据。注意,R | 4 |除了值之外,还有所有这些东西。哇!这真是一场精彩的比赛。我挣扎了几天。只是一个小小的改变。正如您可以看到的,一些组件具有caps(^)字符,这是可选的,也可以出现在字符串中,也可以不出现在字符串中。是否可以修改正则表达式以接受类似于^^^Ca++的组件,并且不包含大写字符,即Ca++?是的,它可以是字符串的一部分,也可以不是字符串的一部分,但如果它出现,则必须与类似于^^^K+的组件一起使用。确定,^正在进行选项化,请立即尝试
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
##############################