简体   繁体   English

Java 正则表达式 - 嵌套对象

[英]Java Regular Expression - Nested Object

I am using Regular Expression in java to separate out groups in nested object as shown below.我在 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-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}
}

The output I am trying to get is我试图得到的输出是

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}

I have tried the expressions below我试过下面的表达

(s*={[^{[]*})+
(\w+={[^{[]+})+

Because of complexity of string in Prop2 , my expression ignores Prop2 .由于Prop2中字符串的复杂性,我的表达式忽略了Prop2 How can I update my expression to capture Prop2 as well.如何更新我的表达式以同时捕获 Prop2。

Try this instead...试试这个...

\w+=\{.*\}

Dot doesn't match newlines.点与换行符不匹配。

This might help:这可能有帮助:

Using Pattern:使用模式:

Pattern.compile("^\\s+(\\w+=\\{.*\\},)", Pattern.MULTILINE)

Pattern in context:上下文中的模式:

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));
}

Output:输出:

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},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM