views:

89

answers:

3

I need a regex expression for matching a string in quotes and then a white space then a round bracket then a curly bracket.

For example this is the text I want to match in Java:

"'Allo 'Allo!" (1982) {A Barrel Full of Airmen (#7.7)}

What would the regex for this be?

Sorry, but I'm just really lost. I tried a lot of different things but now I'm so stumped.

+3  A: 

"[^"]*"\s*\([^)]*\)\s*\{[^}]*\}

Mark Byers
this will fail if the title contains double-quotes :)
vladr
+3  A: 

This should do it:

Pattern p = Pattern.compile("\"(.*?)\"\\s+\\((\\d{4})\\)\\s+\\{(.*?)\\}");
Matcher m = p.matcher("\"'Allo 'Allo!\" (1982) {A Barrel Full of Airmen (#7.7)}");
if (m.find()) {
  System.out.println(m.group());
  System.out.println(m.group(1));
  System.out.println(m.group(2));
  System.out.println(m.group(3));
}

Output:

"'Allo 'Allo!" (1982) {A Barrel Full of Airmen (#7.7)}
'Allo 'Allo!
1982
A Barrel Full of Airmen (#7.7)
cletus
could be more selective on the year (4 digits) to avoid false matches in the case of ill-behaved input
vladr
i want it as one big group not 3 thats the problem....
angad Soni
you mean as your entire original line, i.e. `m.group(0)`?
vladr
@angad: just use `m.group()` to get the whole match...
cletus
oh wow thanks so much guys it works but no i have to make sure it works for my whole text file which is about 20Mb lol but thanks a lot.
angad Soni
hey cletus could you help me in my other question please
angad Soni
A: 

"[^"]+"\s([^)]+)\s{[^}]+}

jsight