fork download
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import java.util.ArrayList;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String regex = "#Section(\\d+)\\b(?:(?!#Section\\d).)*\\bJack/M,(\\d+\\h+[-+]?\\d+(?:\\.\\d+)?(?:\\s+\\d+\\h+[-+]?\\d+(?:\\.\\d+)?)*)";
  12. String string = "#Section250342,Main,First/HS/12345/Jack/M,200010 10.00 200011 -2.00,\n"
  13. + "#Section250322,Main,First/HS/12345/Aaron/N,200010 17.00,\n"
  14. + "#Section250399,Main,First/HS/12345/Jimmy/N,200010 12.00,\n"
  15. + "#Section251234,Main,First/HS/12345/Jack/M,200011 11.00";
  16.  
  17. Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  18. Matcher matcher = pattern.matcher(string);
  19.  
  20. List<String> group1 = new ArrayList<>();
  21. List<String> group2 = new ArrayList<>();
  22. List<String> group3 = new ArrayList<>();
  23.  
  24. while (matcher.find()) {
  25. group1.add(matcher.group(1));
  26. String[] parts = matcher.group(2).split("\\s+");
  27. for (int i = 0; i < parts.length; i++) {
  28. if (i % 2 == 0) {
  29. group2.add(parts[i]);
  30. } else {
  31. group3.add(parts[i]);
  32. }
  33. }
  34. }
  35. System.out.println("Group 1: " + Arrays.toString(group1.toArray()));
  36. System.out.println("Group 2: " + Arrays.toString(group2.toArray()));
  37. System.out.println("Group 3: " + Arrays.toString(group3.toArray()));
  38. }
  39. }
Success #stdin #stdout 0.14s 46932KB
stdin
Standard input is empty
stdout
Group 1: [250342, 251234]
Group 2: [200010, 200011, 200011]
Group 3: [10.00, -2.00, 11.00]