fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String regex = "tree[0-9][a-z]?";
  13. String string = "tree1\n"
  14. + "tree2\n"
  15. + "tree1a\n"
  16. + "tree1b\n"
  17. + "tree2a\n"
  18. + "tree2b\n"
  19. + "atree1\n"
  20. + "tree20\n"
  21. + "tree2ab";
  22.  
  23. Pattern patternSet = Pattern.compile("^tree[0-9][a-z]?$", Pattern.MULTILINE);
  24. Matcher matcher = patternSet.matcher(string);
  25.  
  26. while (matcher.find()) {
  27. System.out.println(matcher.group(0));
  28. }
  29. }
  30. }
Success #stdin #stdout 0.07s 33520KB
stdin
Standard input is empty
stdout
tree1
tree2
tree1a
tree1b
tree2a
tree2b