我正在阅读日志文件,必须提取一行中的所有值,如下所示
> DE-055 LLL[136]
> CHR(_*.....\............B........(............................................12345678.&..g..2%.$.'...3.`(..4.....5.!.6....7.s$...A......S..)
我正在尝试使用正则表达式来提取两个大括号()之间的数据。请告知我如何提取到目前为止,我已经尝试了不同的方法但不能这样做
for (String s : incomingmessage) {
// **DE-011 FXD[006] CHR(008432)**
pattern3 working
String pattern3 = "(DE\\W\\d*)(\\s*\\w*\\W*)(\\d*)(\\W*\\s*) (\\w*)(\\()([\\w*\\s*]+)(\\))"; --> works when the value is like above,same code do not work for the below format
//DE-055 LLL[136] CHR(_*.....\............B........(............................................12345678.&..g..2%.$.'...3.`(..4.....5.!.6....7.s$...A......S..)
String pattern3 = "(DE\\W\\d*)(\\s*\\w*\\W*)(\\d*)(\\W*\\s*)(\\w*)(\\([.*?]\\))"; --> tried many times changing not able to get the regex which will work to get the values between the two braces even if it is some funny characters like the above
Pattern p3 = Pattern.compile(pattern3);
// Matcher m1 = p1.matcher(s);
//Matcher m2 = p2.matcher(s);
Matcher m3 = p3.matcher(s);
while (m3.find()) {
String fullvalue = m3.group(0);
// String field = m3.group(1);
//String length = m3.group(3);
//String value = m3.group(7);
System.out.println("m3.find() full value " + fullvalue);
//System.out.println("m3.find() filed value " + field + "length " + length + "value" + value);
}
}
我希望输出值如下所示,它位于两个大括号()
之间_*.....\............B........(............................................12345678.&..g..2%.$.'...3.`(..4.....5.!.6....7.s$...A......S..
答案 0 :(得分:0)
如果您不希望嵌套大括号,则可以使用:
String input = "sfasdf(sfsdfs76868)dslf()js98(sds),cn,";
String regex = "(?<=\\().*?(?=\\))";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("Match: " + matcher.group(0));
}
输出将是:
Match: sfsdfs76868
Match:
Match: sds
答案 1 :(得分:0)
String t = "DE-055 LLL[136] CHR(_*.....\\............B........(............................................12345678.&..g..2%.$.'...3.`(..4.....5.!.6....7.s$...A......S..)";
String pattern3 = "(DE\\W\\d*)(\\s*\\w*\\W*)(\\d*)(\\W*\\s*)(\\w*)(\\()([\\W*?\\w*?\\s*?\\W*?]+)(\\))";
Output :
group 0 : DE-055 LLL[136] CHR(_*.....\............B........(............................................12345678.&..g..2%.$.'...3.`(..4.....5.!.6....7.s$...A......S..)
group 1 ; DE-055
group 2 : LLL[
group 3 : 136
group 4 : ]
group 5 : CHR
group 6 : (
group 7 : _*.....\............B........(............................................12345678.&..g..2%.$.'...3.`(..4.....5.!.6....7.s$...A......S..
group 8 : )
组7给出了我要查找的输出。 谢谢所有试图帮助我的人。