2

I am trying to extract text within two curly brackets for example given the following string

s = "Some text \test{a}{b}{c} even more " 

I just want to extract all content in b and c as strings. In addition there might be more curly brackets in b and c. I like to extract this text as well.

I have been looking over some functions in Java. As the follwing

String in = "Item(s): \test{x}{y}{z in {X} }";

Pattern p = Pattern.compile("\\{(.*?)\\}");
Matcher m = p.matcher(in);

while(m.find()) {
    System.out.println(m.group(1));
}

But this gives me all content in curly brackets which is

   a="x"
   b="y" 
   c=" z in {X"

Additional question: if the string has multiple different curly bracktes as the following

s1=" Some text \test{a}{b}{c} even more \foo{d}{e}" 

but I still want only b and c. is this easy to implement? For this specific case we can just take iteration 2 and 3 but in general if there is a longer text with multiple \test{}{}{} it is not as simple.

s2 = " Some text \test{a1}{b1}{c1} even more \foo{d}{e} and more \test{a2}{b2}{c2} more plus mehr mas" 

I then want b1, c1 and b2 and c2 For this case we can use a if condition checking for each "\test" and only taking iteration 2 and 3 but that is quite ugly.

A.Dumas
  • 2,619
  • 3
  • 28
  • 51
  • What's the rule? Why do you want to exclude `x`? If you want to ignore the first result, simply ignore it in the first iteration.. – Maroun Apr 09 '15 at 10:00
  • "x" is some string which is unnecessairy. ok I will give it a try. – A.Dumas Apr 09 '15 at 10:06
  • Java's regex doesn't handle nested stuff too well... You'll have to use some kind of state machine (looping through each character, or at least keeping a count of the opening and closing braces you see) to get the correct matches. – Jerry Apr 09 '15 at 10:06
  • Thanks Jerry. This is what i feared. Is there elegant way to go though each character and finding my pattern? – A.Dumas Apr 09 '15 at 10:12
  • http://stackoverflow.com/questions/26974739/split-a-complex-string-based-on-grouping-of-small-brackets-in-java (a bit more compilcated). Parse innermost `(...)` first, via recursion for instance. – Joop Eggen Apr 09 '15 at 10:34

2 Answers2

1

Try StringUtils.substringsBetween if you are using apache commons lang API.

String[] bet = StringUtils.substringsBetween("Some text \test{a}{b}{c} even more ", "{", "}");
System.out.println(Arrays.asList(bet))

;

Aihal
  • 31
  • 1
  • 5
0

If you have only one nested level, you could try:

[{]([^{}]+|[^{}]*[{][^{}]*[}][^{}]*)[}]
Toto
  • 89,455
  • 62
  • 89
  • 125
  • toto. I have tested the code and it has been working well . I post the results when I tested it more. – A.Dumas Apr 09 '15 at 11:07
  • I didn't think too deep earlier. Actually, you can further simplify it to `[{]([^{}]++|[{][^{}]*[}])*[}]` (though this would allow `{}`). – nhahtdh Apr 09 '15 at 14:00