我从连续的字符串缓冲区(从“ 8 = XXX到10 = XXX”)开始获取数据。假设第一次缓冲区扫描的字符串是说:下面是我一次扫描得到的整个字符串。
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”开始的字符串,而其他不匹配的部分只会被呕吐。我也想要剩余的部分。
|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|
在下一次缓冲区扫描中,我将获得字符串,该字符串是模式匹配时呕吐字符串的剩余部分。现在来看,第一次搜索中的呕吐字符串是
8=FIX.4.2|9=00816|35=W49=TT_PRICE
在下一个搜索中,呕吐的字符串是
|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”连接起来并再次解析