在侧花括号中获取字符串,其中的花括号中也包含更多值

时间:2021-01-24 00:08:32

So here's my (java) string

所以这是我的(java)字符串

String s = "Some string preceding this {\"Key1\": \"Val1\", \"Key2\": {\"embedKey1\": \"embedVal1\", \"embedKey2\": \"embedVal2\"}, \"Key3\" : \"Val3\", \"Key3\": \"Val4\"}, some value proceeding it"

I want to get everything that is within the outer curly braces. How do I do that? So far I've tried the following

我想得到外花括号内的所有东西。我怎么做?到目前为止,我已尝试过以下内容

    Pattern p = Pattern.compile("\\{([^}]*)\\}");
    Matcher m = p.matcher(s);
    while(m.find()){
       System.out.println(m.group(1));
    }

This however, only prints

但是,这只是打印

"Key1": "Val1", "Key2": {"embedKey1": "embedVal1", "embedKey2": "embedVal2"

Can someone please help me with this?

有人可以帮我这个吗?

3 个解决方案

#1


2  

To get everything in between outer braces, or between the first { and the last }, use greedy matching with a . matching all symbols (with DOTALL mode):

要在外括号之间或第一个{和最后一个}之间获取所有内容,请使用与a的贪婪匹配。匹配所有符号(使用DOTALL模式):

String s = "Some string preceding this {\"Key1\": \"Val1\", \"Key2\": {\"embedKey1\": \"embedVal1\", \"embedKey2\": \"embedVal2\"}, \"Key3\" : \"Val3\", \"Key3\": \"Val4\"}, some value proceeding it";
Pattern p = Pattern.compile("(?s)\\{(.*)}");
Matcher m = p.matcher(s);
while(m.find()){
    System.out.println(m.group(1));
}

See IDEONE demo

请参阅IDEONE演示

The (?s) is an inline version of the Pattern.DOTALL modifier.

(?s)是Pattern.DOTALL修饰符的内联版本。

#2


0  

For your particular example you can use a regex like this:

对于您的特定示例,您可以使用这样的正则表达式:

\{(.*?)\{.*?}(.*?)}

Working demo

工作演示

Match information

匹配信息

MATCH 1
1.  [40-64] `"Key1": "Val1", "Key2": `
2.  [116-149]   `, "Key3" : "Val3", "Key3": "Val4"`

On the other hand, if you want to even capture the inner curly braces you can use something easier like this:

另一方面,如果你想要捕捉内部花括号,你可以使用更简单的东西:

\{(.*)}

Working demo

工作演示

Match information

匹配信息

MATCH 1
1.  [40-149]    `"Key1": "Val1", "Key2": {"embedKey1": "embedVal1", "embedKey2": "embedVal2"}, "Key3" : "Val3", "Key3": "Val4"`
QUICK REFERENCE

Remember to escape backslashes in java:

记得在java中转义反斜杠:

Pattern p = Pattern.compile("\\{(.*)}");
Matcher m = p.matcher(s);
while(m.find()){
   System.out.println(m.group(1));
}

#3


0  

If It's just about outermost "{" and "}" as you mentioned in your problem, this may be one of the non-regex approach.

如果你在问题中提到它只是最外面的“{”和“}”,这可能是非正则表达式之一。

    int first=s.indexOf('{');
    int last=s.lastIndexOf('}');

    String result=s.substring(first, last+1);

#1


2  

To get everything in between outer braces, or between the first { and the last }, use greedy matching with a . matching all symbols (with DOTALL mode):

要在外括号之间或第一个{和最后一个}之间获取所有内容,请使用与a的贪婪匹配。匹配所有符号(使用DOTALL模式):

String s = "Some string preceding this {\"Key1\": \"Val1\", \"Key2\": {\"embedKey1\": \"embedVal1\", \"embedKey2\": \"embedVal2\"}, \"Key3\" : \"Val3\", \"Key3\": \"Val4\"}, some value proceeding it";
Pattern p = Pattern.compile("(?s)\\{(.*)}");
Matcher m = p.matcher(s);
while(m.find()){
    System.out.println(m.group(1));
}

See IDEONE demo

请参阅IDEONE演示

The (?s) is an inline version of the Pattern.DOTALL modifier.

(?s)是Pattern.DOTALL修饰符的内联版本。

#2


0  

For your particular example you can use a regex like this:

对于您的特定示例,您可以使用这样的正则表达式:

\{(.*?)\{.*?}(.*?)}

Working demo

工作演示

Match information

匹配信息

MATCH 1
1.  [40-64] `"Key1": "Val1", "Key2": `
2.  [116-149]   `, "Key3" : "Val3", "Key3": "Val4"`

On the other hand, if you want to even capture the inner curly braces you can use something easier like this:

另一方面,如果你想要捕捉内部花括号,你可以使用更简单的东西:

\{(.*)}

Working demo

工作演示

Match information

匹配信息

MATCH 1
1.  [40-149]    `"Key1": "Val1", "Key2": {"embedKey1": "embedVal1", "embedKey2": "embedVal2"}, "Key3" : "Val3", "Key3": "Val4"`
QUICK REFERENCE

Remember to escape backslashes in java:

记得在java中转义反斜杠:

Pattern p = Pattern.compile("\\{(.*)}");
Matcher m = p.matcher(s);
while(m.find()){
   System.out.println(m.group(1));
}

#3


0  

If It's just about outermost "{" and "}" as you mentioned in your problem, this may be one of the non-regex approach.

如果你在问题中提到它只是最外面的“{”和“}”,这可能是非正则表达式之一。

    int first=s.indexOf('{');
    int last=s.lastIndexOf('}');

    String result=s.substring(first, last+1);