fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. Pattern p;
  14. try {
  15. p = Pattern.compile("\\[(?<text>[^\\]]*)\\]\\((?<link>[^\\)]*)\\)");
  16. } catch (PatternSyntaxException ex) {
  17. System.out.println(ex);
  18. throw(ex);
  19. }
  20. Matcher m1 = p.matcher("Hello");
  21. Matcher m2 = p.matcher("Hello [world](ladies)");
  22. Matcher m3 = p.matcher("Well, [this](that) has [two](too many) keys.");
  23. System.out.println("m1 matches: " + m1.matches());
  24. System.out.println("m2 matches: " + m2.matches());
  25. System.out.println("m3 matches: " + m3.matches());
  26. m2.start();
  27. m3.start();
  28. System.out.println("m2 text: " + m2.group("text"));
  29. System.out.println("m2 link: " + m2.group("link"));
  30. System.out.println("m3 text: " + m3.group("text"));
  31. System.out.println("m3 link: " + m3.group("link"));
  32. System.out.println("m3 count: " + m3.groupCount());
  33. System.out.println("m3 find: " + m3.find());
  34. }
  35. }
Runtime error #stdin #stdout #stderr 0.07s 380224KB
stdin
Standard input is empty
stdout
m1 matches: false
m2 matches: false
m3 matches: false
stderr
Exception in thread "main" java.lang.IllegalStateException: No match available
	at java.util.regex.Matcher.start(Matcher.java:342)
	at Ideone.main(Main.java:26)