fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. Pattern p=Pattern.compile("(.*?)\\s*(tbsp|k?g|cup|c?m)\\s*(.*)");
  12. List<String> tests=Arrays.asList(
  13. "16g salt",
  14. "1 kg apple",
  15. "1 1/2 tbsp sugar");
  16. for(String s:tests){
  17. Matcher m=p.matcher(s);
  18. if(m.matches())
  19. System.out.println(Arrays.asList(m.group(1),m.group(2),m.group(3)));
  20. }
  21. }
  22. }
Success #stdin #stdout 0.09s 33500KB
stdin
Standard input is empty
stdout
[16, g, salt]
[1, kg, apple]
[1 1/2, tbsp, sugar]