fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. String s = "NameJJ,ABC/firstname,120, NameBB,ABC/secondname,100,NameCC,ABC/thirdname,150,NameJJ,ABC/firstname,200,NameBB,ABC/secondname,300,NameCC,ABC/thirdname,500";
  17.  
  18. List<Integer> listNameJJ = getList("(\\w+)JJ,(\\w+)\\/(\\w+),(\\d+)", s);
  19. List<Integer> listNameBB = getList("(\\w+)BB,(\\w+)\\/(\\w+),(\\d+)", s);
  20. List<Integer> listNameCC = getList("(\\w+)CC,(\\w+)\\/(\\w+),(\\d+)", s);
  21.  
  22. System.out.println(listNameJJ);
  23. System.out.println(listNameBB);
  24. System.out.println(listNameCC);
  25. }
  26.  
  27. public static List<Integer> getList(String regex, String str) {
  28. Pattern pattern = Pattern.compile(regex);
  29. Matcher matcher = pattern.matcher(str);
  30. List<Integer> list = new ArrayList<>();
  31.  
  32. while (matcher.find()) {
  33. list.add(Integer.valueOf(matcher.group(4)));
  34. }
  35.  
  36. return list;
  37. }
  38. }
Success #stdin #stdout 0.09s 52292KB
stdin
Standard input is empty
stdout
[120, 200]
[100, 300]
[150, 500]