import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:\\d\\h+|[\\d.]*[A-Z][A-Z\\d.]*\\h+)*[A-Z]{3,}";
final String string = "ID.4 ESTATE 109kW City Pure 52kWh 5dr Auto\n"
+ "XC40 DIESEL ESTATE 2.0 D4 [190] Inscription Pro 5dr AWD Gea+\n"
+ "RAPID DIESEL HATCHBACK 1.6 TDI CR Elegance 5dr\n"
+ "MODEL 3 120kW LONG RANGE AWD \n"
+ "V40 DIESEL HATCHBACK D2 [120] Momentum 5dr Geartronic\n"
+ "V60 DIESEL SPORTSWAGON 2.0 D4 [190] R DESIGN Plus 5dr Auto\n"
+ "X3 ESTATE xDrive M40i 5dr Auto\n"
+ "C CLASS DIESEL SALOON C250 BlueTEC SE 4dr Auto\n"
+ "A CLASS DIESEL HATCHBACK A180d Sport Executive 5dr Auto\n"
+ "2 SERIES COUPE 218i Sport 2dr [Nav] Step Auto\n"
+ "HILUX SPECIAL EDITIONS Invincible X Ltd Ed D/Cab P/Up 2.4 D+\n"
+ "C3 PICASSO 1.6 BlueHDi Platinum 5dr\n"
+ "C3 HATCHBACK 1.2 PureTech 82 Flair 5dr\n"
+ "XC40 DIESEL ESTATE XC40\n"
+ "1";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html