fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void getAllMatches(Pattern p, String in, List<String> out)
  12. {
  13. Matcher m = p.matcher(in);
  14. while (m.find())
  15. {
  16. out.add(m.group(2));
  17. getAllMatches(p, m.group(2), out);
  18. }
  19. }
  20.  
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23. Pattern p = Pattern.compile("\\[([^\\]]+)\\](.+?)\\[/\\1\\]");
  24. List<String> out = new ArrayList<String>();
  25. String s = "[A]outer[B][C]last one left[/C]middle[/B][/A] [A]out[B]in[/B][/A]";
  26. getAllMatches(p, s, out);
  27.  
  28. // output stuff
  29. System.out.println("IN: "+s+"\n-----------");
  30. for (String o : out) { System.out.println(o); }
  31. s = "[quote]http://w...content-available-to-author-only...e.com?watch?v=asasdsadsa [url]aisa[/url] [/quote]";
  32. System.out.println("-----------\nIN: "+s+"\n-----------");
  33. out.clear(); getAllMatches(p, s, out);
  34. for (String o : out) { System.out.println(o); }
  35. }
  36. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
IN: [A]outer[B][C]last one left[/C]middle[/B][/A] [A]out[B]in[/B][/A]
-----------
outer[B][C]last one left[/C]middle[/B]
[C]last one left[/C]middle
last one left
out[B]in[/B]
in
-----------
IN: [quote]http://w...content-available-to-author-only...e.com?watch?v=asasdsadsa [url]aisa[/url] [/quote]
-----------
http://w...content-available-to-author-only...e.com?watch?v=asasdsadsa [url]aisa[/url] 
aisa