fork(5) 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 main (String[] args) throws java.lang.Exception
  12. {
  13. String regex = "(?m)^\\h*/\\*(?!((?!/\\*)[\\s\\S])*\\*/).*";
  14. String string = "How do I exclude multiple characters at once.\n\n"
  15. + "I need to write regex that can check if a java multi-line comment has no end.\n\n"
  16. + "For example two comments\n\n"
  17. + "/* some comment */\n"
  18. + "and\n\n"
  19. + "/* Multi line comment start\n"
  20. + "The regex code must match the 2nd comment but not the first.\n"
  21. + "/* some comment \n"
  22. + "I tried something like\n\n"
  23. + "\\/\\*(.*)[^\\*\\/]\n"
  24. + "but this only excludes one character. How do I exclude two at once.";
  25.  
  26. Pattern pattern = Pattern.compile(regex);
  27. Matcher matcher = pattern.matcher(string);
  28.  
  29. while (matcher.find()) {
  30. System.out.println("Full match: " + matcher.group(0));
  31. for (int i = 1; i <= matcher.groupCount(); i++) {
  32. System.out.println("Group " + i + ": " + matcher.group(i));
  33. }
  34. }
  35. }
  36. }
Success #stdin #stdout 0.08s 27940KB
stdin
Standard input is empty
stdout
Full match: /* Multi line comment start
Group 1:  
Full match: /* some comment 
Group 1: null