fork(36) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.io.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. String text = "This_is_a_[sample]_text[notworking]";
  15. Matcher matcher = Pattern.compile("\\[([a-zA-Z_]+)\\]").matcher(text);
  16. int i = 0;
  17. while (matcher.find()) {
  18. for (int j = 0; j <= matcher.groupCount(); j++) {
  19. System.out.println("------------------------------------");
  20. System.out.println("Group " + i + ": " + matcher.group(j));
  21. i++;
  22. }
  23. }
  24. }
  25. }
Success #stdin #stdout 0.1s 321600KB
stdin
Standard input is empty
stdout
------------------------------------
Group 0: [sample]
------------------------------------
Group 1: sample
------------------------------------
Group 2: [notworking]
------------------------------------
Group 3: notworking