Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 在try中调用matcher函数,同时使用GNU读取连续数据_Java_Regex_Serial Port_Try Catch_Matcher - Fatal编程技术网

Java 在try中调用matcher函数,同时使用GNU读取连续数据

Java 在try中调用matcher函数,同时使用GNU读取连续数据,java,regex,serial-port,try-catch,matcher,Java,Regex,Serial Port,Try Catch,Matcher,在上述SerialPortEvent.Dat_可用的切换情况下,我实时接收连续数据。matcher函数调用下面定义的函数 case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[64]; try { // read data int numBytes = inputStream.read(readBuffer); inputS

在上述SerialPortEvent.Dat_可用的切换情况下,我实时接收连续数据。matcher函数调用下面定义的函数

case SerialPortEvent.DATA_AVAILABLE:

      byte[] readBuffer = new byte[64];
     try {
            // read data
            int numBytes = inputStream.read(readBuffer);
            inputStream.close();
            //-------------------------------
           //send the received data to the GUI
            String result = new String(readBuffer,0,numBytes);
            //-----------------------------
            gui.setjtaReceived(result);

            matcher(result,writer,df);
            //gui.setjtaReceived(result);
     }
     catch (IOException e) {exceptionReport(e);}
当我尝试写入一个外部csv文件,甚至尝试执行System.out.println(m1.group)或System.out.println(match_heartBeat)时,我无法将其写入文件或打印到屏幕。然而,System.out.println(m1)是在屏幕上打印的。你知道如何克服这个问题吗?我正在尝试实时解码接收到的数据。 模式如下:

    private void matcher(String str,FileWriter writer,DateFormat df) {
    Matcher m1 = p1.matcher(str);
    Matcher m2 = p2.matcher(str);
    System.out.println(m1.group());
    Calendar cal = Calendar.getInstance();
    String match_heartBeat = null;
    String match1 = m1.group();
    int length1 = match1.length();
    if(m2.find()){
        String match2 = m2.group();
        int length2 = match2.length();
        match_heartBeat = match2.substring(2, length2-1);
        //System.out.println(match1.subSequence(2, 4) + ";" + match_heartBeat);
    }
    String realTime = df.format(cal.getTime());
    writer.append(realTime);
    writer.append(',');
    writer.append(match1.subSequence(2, length1-1));
    writer.append(',');
    writer.append(match_heartBeat);
    writer.append('\n');
    writer.flush();


 }
它查找字母“a”直到空格和“到空格”。一旦程序开始运行,就会生成文件“writer”。但是可以附加解码的数据

样本数据:

    Pattern p1 = Pattern.compile("\\b(a)\\w*( )\\b");
    Pattern p2 = Pattern.compile("\\b(')\\w*( )\\b");
样本输出 CSV文件


让我们根据评论澄清规范。这是输入字符串:

System time , 17 , 00
790009A017 009a047

这是输出字符串:

System time , 17 , 00
“017”是一个十进制(不是八进制)数。以后可以方便地将其格式化为“17”

如评论中所述,此正则表达式不正确:

017,00
应该是:

Pattern p1 = Pattern.compile("\\b(a)\\w*( )\\b");
快速演示:

Pattern p1 = Pattern.compile("a(\\d+) (\\d{2})");

你说的“字母表a”是什么意思
(a)
将只捕获组中的
a
字符,如果您不想使用a,甚至不需要括号。空间和引用也是一样,你也没有说出你想要捕捉什么。给出示例输入数据和您想要的结果。上面的字符串将是示例数据的一部分。在a之后将有017和一个空格。这个字符串是通过串口从设备读取的。哦,那么我的模式将如何定义呢?谢谢,请编辑你的文章,包括样本数据和你想从中得到什么
echo "79 0009a017 009a047" | perl -lne 'print $1,",",$2 if /a(\d+) (\d{2})/'
017,00