fork 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 = "custom onetomany manytomany atom tomcat tomorrow automatic tom tomahawk";
  10. Pattern regex = Pattern.compile("custom|onetomany|manytomany|atom|tomcat|tomorrow|automatic|(\\w*tom\\w*)");
  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. System.out.println("\n" + "*** Matches ***");
  22. if(group1Caps.size()>0) {
  23. for (String match : group1Caps) System.out.println(match);
  24. }
  25.  
  26. } // end main
  27. } // end Program
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
*** Matches ***
tom
tomahawk