tags:

views:

43

answers:

2

Is there a way to get first part of a String before 4 numbers in ().

Input String: "Some Title (2000) some text."
Output String: "Some Title "

I don't want to iterate over matches and get first. I want regex to get the characters before 4 numbers in () and I want it to discard the rest of the text.

+2  A: 

The Regexp would be something like

(.*)\(\d{4}\).*

For usage in Java you need to escape backshlashes and the output string is at group 1.

GHad
+1  A: 

For exactly this type of text:

String result = input.split("\\(")[0];

or, if ( may occur in the first part:

String result = input.split("\\(\\d{4}\\)")[0];

This even works for inputs containing no number at all and empty strings.

Andreas_D