【发布时间】:2022-02-04 20:40:00
【问题描述】:
我想获取所有的消息数据。这样它应该在父消息的大括号之间查找消息和所有数据。使用下面的模式,我没有得到所有的父体。
String data = "syntax = \"proto3\";\r\n" +
"package grpc;\r\n" +
"\r\n" +
"import \"envoyproxy/protoc-gen-validate/validate/validate.proto\";\r\n" +
"import \"google/api/annotations.proto\";\r\n" +
"import \"google/protobuf/wrappers.proto\";\r\n" +
"import \"protoc-gen-swagger/options/annotations.proto\";\r\n" +
"\r\n" +
"message Acc {\r\n" +
" message AccErr {\r\n" +
" enum Enum {\r\n" +
" UNKNOWN = 0;\r\n" +
" CASH = 1;\r\n" +
" }\r\n" +
" }\r\n" +
" string account_id = 1;\r\n" +
" string name = 3;\r\n" +
" string account_type = 4;\r\n" +
"}\r\n" +
"\r\n" +
"message Name {\r\n" +
" string firstname = 1;\r\n" +
" string lastname = 2;\r\n" +
"}";
List<String> allMessages = new ArrayList<>();
Pattern pattern = Pattern.compile("message[^\\}]*\\}");
Matcher matcher = pattern.matcher(data);
while (matcher.find()) {
String str = matcher.group();
allMessages.add(str);
System.out.println(str);
}
}
我希望在我的大小为 2 的字符串数组列表中得到如下响应。
allMessage.get(0) 应该是:
message Acc {
message AccErr {
enum Enum {
UNKNOWN = 0;
CASH = 1;
}
}
string account_id = 1;
string name = 3;
string account_type = 4;
}
而allMessage.get(1) 应该是:
message Name {
string firstname = 1;
string lastname = 2;
}
【问题讨论】:
-
我想这样就可以了..stackoverflow.com/questions/47162098/…
-
您的问题是您在寻找的那个之前点击了
}令牌,例如关闭enum的那个。