在正则表达式JAVA中使用模式匹配器时,如何获取剩余的不匹配字符串? [英] How to get the remaining not matched string when using Pattern Matcher in regex JAVA?

查看:253
本文介绍了在正则表达式JAVA中使用模式匹配器时,如何获取剩余的不匹配字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从连续的字符串缓冲区(从 8 = XXX到10 = XXX)开始获取数据。假设第一次缓冲区扫描的字符串是:下面是我一次扫描得到的整个字符串。

I am getting data from a continuous buffer of strings staring from "8=XXX to 10=XXX". For suppose the string for the first buffer scan is say :Below is the entire string I got in one scan.

8=FIX.4.2|9=00815|35=W|49=TT_PRICE|56=SAP0094X|10=134| 
8=FIX.4.2|9=00816|35=W49=TT_PRICE  ----------------here I didn't get the full string

现在,我要从 8 = xxx开始并以 10 = xxx |结束的字符串。我已经为此编写了一个程序,并且运行正常。现在的问题是,当我传递上述字符串进行匹配时,我只会得到恰好从 8 = xxx到10 = xxx开始的字符串,而其他不匹配的部分只会被呕吐。我也想要剩余的部分。

Now I want the string starting from "8=xxx" and ending with "10=xxx|" . I have written a program for that and it's working fine. Now the problem is when I pass the above string for matching I only get the string that is exactly starting from "8=xxx to 10=xxx" and the other part that is not match just gets vomited. I also want the remaining part.

|56=SAP0094X|10=134|-------This is the remaining part of the above vomited string
8=FIX.4.2|9=00815|35=W|49=TT_PRICE|56=SAP0094X|10=134|

在下一次缓冲区扫描中,我将得到字符串,该字符串是pattern时呕吐的字符串的剩余部分匹配。现在来看,第一次搜索中呕吐的字符串是

In the next buffer scan I will get the string which is the remaining part of the vomited string while pattern matching. Now see , the vomited string in the first search is

8=FIX.4.2|9=00816|35=W49=TT_PRICE

,下次搜索时呕吐的字符串是

and the vomited string in the next search is

|56=SAP0094X|10=134|

这两个字符串都必须像

8=FIX.4.2|9=00816|35=W49=TT_PRICE|56=SAP0094X|10=134|

这是完整的字符串。

下面是我的代码:

String text = in.toString(CharsetUtil.UTF_8); //in is a reference to ByteBuf
     Pattern r = Pattern.compile("(8=\\w\\w\\w)[\\s\\S]*?(10=\\w\\w\\w)");
     Matcher m = r.matcher(text);

      while (m.find()) {
          String message = m.group();
          // I need to get the remaining not matched string and has to be appended to the not matched string in the next search so that I will be getting the whole string starting from "8=xxx to 10=xxx|"
          System.out.println("Incoming From Exchange >> "+message);
      }


推荐答案

您可以为此使用组:

public static void main(String[] args) {
    String someInput = "XXX-payload-YYY-some-tail";
    Pattern r = Pattern.compile("(XXX)(.*)(YYY)(.*)");
    Matcher m = r.matcher(someInput);

    if (m.matches()) {
        System.out.println("initial token: " + m.group(1));
        System.out.println("payload: " + m.group(2));
        System.out.println("end token: " + m.group(3));
        System.out.println("tail: " + m.group(4));
    }
}

输出:

initial token: XXX 
payload: -payload- 
end token: YYY
tail: -some-tail

然后您可以将 tail与第二次扫描的结果连接起来并再次解析

Than you can concatenate the "tail" with a result of the second scan and parse it again

这篇关于在正则表达式JAVA中使用模式匹配器时,如何获取剩余的不匹配字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆