fork download
  1. import java.io.*;
  2. import java.util.regex.*;
  3.  
  4. class Ideone
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. String input = "2 ltr. btl., select varieties when you buy 6 $1.25 ea.-50ยข MIX OR MATCH";
  9. Pattern pattern = Pattern.compile(
  10. "(?x)\\d+(?:\\.\\d+)?\\s+"
  11. + " (?:"
  12. + " (?:fl )?oz(?:\\.|\\b)|lbs?(?:\\.|\\b)|kg(?:\\.|\\b)|kg?\\b|g(?:\\.|\\b)"
  13. + " | pc?k(?:\\.|\\b)|ea(?:\\.|\\b)|ml(?:\\.|\\b)|[cq]t(?:\\.|\\b)"
  14. + " | liter\\b|ltr(?:\\.|\\b)"
  15. + " )"
  16. );
  17. Matcher matcher = pattern.matcher(input);
  18. while (matcher.find()) {
  19. System.out.println(matcher.group());
  20. }
  21. }
  22. }
  23.  
Success #stdin #stdout 0.05s 320576KB
stdin
Standard input is empty
stdout
2 ltr.
1.25 ea.