import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\d+?[\\+x])?\\d+ ?(litros?|u(ni)?ds?|kg|g|cl)(.?, \\w+ \\d+ ?(litros?|u(ni)?ds|kg|g))?";
final String string = "Aceite de oliva virgen extra CARBONELL, garrafa 3 litros\n"
+ "Aceite de oliva virgen CARBONELL, botella 1 litro\n"
+ "First Bike, bici sin pedales roja,edad rec:2-5 años CHICCO, peso máximo 25 Kg\n"
+ "Bombón de almendras MAGNUM, 3+1 uds., caja 423 g\n"
+ "Bote PLAY-DOH,pasta de moldear, sutidos sorpresa, edad rec:+2 años, pack 4uds\n"
+ "Magdalena 0% azúcar LA BELLA EASO, 8 unid., paquete 232 g\n"
+ "Magdalena 0% azúcar LA BELLA EASO, 8 unid., paquete 232 g\n"
+ "Cerveza MAHOU 5 Estrellas, pack botellín 12x25 cl\n"
+ "Bote PLAY-DOH,pasta de moldear, sutidos sorpresa, edad rec:+2 años, pack 4uds";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
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