Java正则表达式-嵌套对象
我在java中使用正则表达式来分离嵌套对象中的组,如下所示Java正则表达式-嵌套对象,java,regex,Java,Regex,我在java中使用正则表达式来分离嵌套对象中的组,如下所示 properties={ prop1={boolean=null, string=null, byte=null, double=null, propertyType=integer, short=null, integer=1, float=null, long=null}, prop2={boolean=null, string=appId=854*tmp=159347540*temp4=469db7a0-d416
properties={
prop1={boolean=null, string=null, byte=null, double=null, propertyType=integer, short=null, integer=1, float=null, long=null},
prop2={boolean=null, string=appId=854*tmp=159347540*temp4=469db7a0-d416-4fa5-856e-e26c33532559d*ts=1604594951147*btid=130324792*guid=72ee83438-6ece-40a4-9da4-2712fb296977*exitguid=13*unresolvedexitid=15015439*cidfrom=32324647*etypeorder=JMS*esubtype=JMS*cidto={[UNRESOLVED][150342]}, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null},
prop3={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null},
prop4={boolean=null, string=TEST, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null},
prop5={boolean=null, string=TEST2, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null},
prop6={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null}
}
我想要得到的结果是
Group1 ---> prop1={boolean=null, string=null, byte=null, double=null, propertyType=integer, short=null, integer=1, float=null, long=null},
Group2 ---> prop2={boolean=null, string=appId=854*tmp=159347540*temp4=469db7a0-d416-4fa5-856e-e26c33532559d*ts=1604594951147*btid=130324792*guid=72ee83438-6ece-40a4-9da4-2712fb296977*exitguid=13*unresolvedexitid=15015439*cidfrom=32324647*etypeorder=JMS*esubtype=JMS*cidto={[UNRESOLVED][150342]}, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null},
Group3 ---> prop3={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null},
Group4 ---> prop4={boolean=null, string=TEST, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null},
Group5 --->prop5={boolean=null, string=TEST2, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null},
Group6 ---> prop6={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null}
我试过下面的表达
(s*={[^{[]*})+
(\w+={[^{[]+})+
由于Prop2中字符串的复杂性,我的表达式忽略了Prop2。如何更新表达式以同时捕获Prop2。试试这个
\w+=\{.*\}
点与换行符不匹配。请尝试此选项
\w+=\{.*\}
点与换行符不匹配。这可能会有所帮助: 使用模式:
Pattern.compile("^\\s+(\\w+=\\{.*\\},)", Pattern.MULTILINE)
上下文中的模式:
public static void main(String[] args) {
String input = "properties={\n"
+ " prop1={boolean=null, string=null, byte=null, double=null, propertyType=integer, short=null, integer=1, float=null, long=null}, \n"
+ " prop2={boolean=null, string=appId=854*tmp=159347540*temp4=469db7a0-d416-4fa5-856e-e26c33532559d*ts=1604594951147*btid=130324792*guid=72ee83438-6ece-40a4-9da4-2712fb296977*exitguid=13*unresolvedexitid=15015439*cidfrom=32324647*etypeorder=JMS*esubtype=JMS*cidto={[UNRESOLVED][150342]}, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null}, \n"
+ " prop3={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null}, \n"
+ " prop4={boolean=null, string=TEST, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null}, \n"
+ " prop5={boolean=null, string=TEST2, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null}, \n"
+ " prop6={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null}\n"
+ "}";
Matcher matcher = Pattern.compile("^\\s+(\\w+=\\{.*\\},)", Pattern.MULTILINE).matcher(input);
List<String> result = new ArrayList<>();
while(matcher.find()) {
result.add(matcher.group(1));
}
result.forEach(s -> System.out.println(s));
}
这可能会有所帮助: 使用模式:
Pattern.compile("^\\s+(\\w+=\\{.*\\},)", Pattern.MULTILINE)
上下文中的模式:
public static void main(String[] args) {
String input = "properties={\n"
+ " prop1={boolean=null, string=null, byte=null, double=null, propertyType=integer, short=null, integer=1, float=null, long=null}, \n"
+ " prop2={boolean=null, string=appId=854*tmp=159347540*temp4=469db7a0-d416-4fa5-856e-e26c33532559d*ts=1604594951147*btid=130324792*guid=72ee83438-6ece-40a4-9da4-2712fb296977*exitguid=13*unresolvedexitid=15015439*cidfrom=32324647*etypeorder=JMS*esubtype=JMS*cidto={[UNRESOLVED][150342]}, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null}, \n"
+ " prop3={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null}, \n"
+ " prop4={boolean=null, string=TEST, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null}, \n"
+ " prop5={boolean=null, string=TEST2, byte=null, double=null, propertyType=string, short=null, integer=null, float=null, long=null}, \n"
+ " prop6={boolean=true, string=null, byte=null, double=null, propertyType=boolean, short=null, integer=null, float=null, long=null}\n"
+ "}";
Matcher matcher = Pattern.compile("^\\s+(\\w+=\\{.*\\},)", Pattern.MULTILINE).matcher(input);
List<String> result = new ArrayList<>();
while(matcher.find()) {
result.add(matcher.group(1));
}
result.forEach(s -> System.out.println(s));
}
听起来你不需要正则表达式,但是需要一个用于结构化数据的普通解析器。你的正则表达式中的任何一个都是有效的。你还没有摆脱牙套。请更正。欢迎使用SO!这个例子似乎过于琐碎(只需在换行符上拆分并切掉第一行和最后一行),但很容易看出,一旦一个组变为多行,非递归正则表达式就无法解析这个问题。请提供更多信息,使计算类在这里可以识别。您想要什么样的嵌套?一些单独的
prop={
行具有嵌套。而外部的properties={
具有所有这些内部属性。您如何定义从何处开始?只需注意组0匹配数据(?s)prop\d+=(?={)(?:(?=.{.....*.\1)(.}....*\2.*)(?=...}......*.......*.\2)(*))+++.+.+.+.+.+.+.+.[[.+.+.+.+.+.[[[[[[[[[[]1=\2$)
听起来你不想要正则表达式,但需要一个用于结构化数据的普通解析器。你的正则表达式中的任何一个都是有效的。例如,你没有逃过大括号。请更正它们。欢迎这样做!示例似乎过于琐碎(只需在换行符上拆分并切掉第一行和最后一行)但是很容易看出,当一个组变为多行时,非递归正则表达式就无法解析该问题。请提供更多信息,使计算类在此处可识别。您要寻找什么样的嵌套?一些单独的prop={/code>行具有嵌套。以及外部属性={
包含所有这些内部参数。您如何定义从何处开始?只需注意组0匹配数据(?s)prop\d+=(?={)(?:(?=.*.{(?。*.?\1)(.*.}(?。.*..*\2.*)(?=..}(?.*....*?\2)(.*)+?=\1)[^{.*(?=\2$)(?=\1)[^{.*.++++.[{code>