fork(21) download
  1. import java.util.*;
  2. import java.io.*;
  3. import java.util.regex.*;
  4. import java.util.List;
  5.  
  6. class Program {
  7. public static void main (String[] args) throws java.lang.Exception {
  8.  
  9. String subject = "12,44,foo,bar,(23,45,200),6";
  10. Pattern regex = Pattern.compile("\\(.*?\\)|(,)");
  11. Matcher regexMatcher = regex.matcher(subject);
  12. List<String> group1Caps = new ArrayList<String>();
  13.  
  14. // put Group 1 captures in a list
  15. while (regexMatcher.find()) {
  16. if(regexMatcher.group(1) != null) {
  17. group1Caps.add(regexMatcher.group(1));
  18. }
  19. } // end of building the list
  20.  
  21. // What are all the matches?
  22. System.out.println("\n" + "*** Matches ***");
  23. if(group1Caps.size()>0) {
  24. for (String match : group1Caps) System.out.println(match);
  25. }
  26. } // end main
  27. } // end Program
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
*** Matches ***
,
,
,
,
,