fork(2) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. Matcher m1 = Pattern.compile("(test1)(test2)?(test3)").matcher("test1test3");
  11. if(m1.matches()) {
  12. for(int g=1;g<=m1.groupCount();g++){
  13. System.out.println("Group "+g+": "+m1.group(g));
  14. }
  15. }
  16. System.out.println("--------------");
  17. Matcher m2 = Pattern.compile("(test1)()(test3)").matcher("test1test3");
  18. if(m2.matches()) {
  19. for(int g=1;g<=m2.groupCount();g++){
  20. System.out.println("Group "+g+": "+m2.group(g));
  21. }
  22. }
  23. }
  24. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
Group 1: test1
Group 2: null
Group 3: test3
--------------
Group 1: test1
Group 2: 
Group 3: test3