-1

I need to extract a String between two {}. For example, .split(regex)

{Regex is difficult.}fadsfjkaslfdjsa{Humbug}asfasdfjaskdlfjlkaf 

should return an array with Regex is difficult. as the first entry and Humbug as the second.

How would I write a regex to do this in Java?

I have tried the answers below, but I want to use myString.split(regex). I probably should have been more specific in my answer.

Should I even be using .split() for this or is there another way?

Skylion
  • 2,696
  • 26
  • 50

2 Answers2

5
String regex = "\\{([^}]*)\\}"

\\{ and \\} escape { and }, respectively.
([^}]*) captures everything after the { and all characters following, but not including }.
\\} at the end requires that there is a }

Alternatively a non-greedy capture term works, ie (.*?); however, I think that the character class is easier to understand for a beginner.

EDIT:
To extract the contents, simply do as follows:

Matcher m = Pattern.compile("\\{([^}]*)\\}").matcher(myString);

while (m.find()) 
{
     myArrayList.add(m.group(1)); 
}
Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • I think, non-greedy mode is dead simple too... but it's just my opinion. – Display Name Nov 10 '13 at 20:02
  • @SargeBorsch I agree, it's not complicated, but if you know nothing about regex, the character class way is probably a little easier to understand. – Steve P. Nov 10 '13 at 20:03
  • "\\{([^}]*)\\}" may be better to capture empty strings b/w braces. – Stefanov.sm Nov 10 '13 at 20:04
  • @StefanStefanov Good call. – Steve P. Nov 10 '13 at 20:04
  • When I try myString.split("\\{([^}]+)\\}"), I get everything except the text I actually want... – Skylion Nov 10 '13 at 20:04
  • @Skylion You didn't say that you wanted to use `.split()`. Using the regular expression that I have will allow you to extract what's in between the `{}`, through the capturing group. – Steve P. Nov 10 '13 at 20:06
  • I updated the question to state that. – Skylion Nov 10 '13 at 20:07
  • 1
    @Skylion This is **not** a good application for `.split()`. I would just use this regex and manually create array entries if that is required... – Steve P. Nov 10 '13 at 20:08
  • What would be a good application of .split() then? – Skylion Nov 10 '13 at 20:11
  • @Skylion Say you're trying to break up a `String` by using `;` or `:` as delimiters. Then your regex would be `:|;` and that would be fine. I updated my answer to try to help you out for how to do what you want to do. – Steve P. Nov 10 '13 at 20:13
  • You probably should use ArrayList here since we don't know how many matching substrings will be in user data. Then you can convert it to String array if needed. – Pshemo Nov 10 '13 at 20:13
  • @Pshemo Yes, that would be preferable. Edit made. – Steve P. Nov 10 '13 at 20:15
1

You can't deal with nested structures with regex (this will summon demons), but if they are only 1-level, then you can match them with
\{(.*?)\}

And regex is not difficult.

Community
  • 1
  • 1
Display Name
  • 8,022
  • 3
  • 31
  • 66