0

I am currently doing comment analysis with some Java code and needed to do the following opperation:

comment = comment.replaceAll("/**", "");

But I am meet with this Exception:

    Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 2
/**
  ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at commentCoupler.coupler.commentCriteriaForfilled(coupler.java:234)
    at commentCoupler.coupler.analyze(coupler.java:64)
    at commentCoupler.coupler.recursiveCoupler(coupler.java:27)
    at commentCoupler.coupler.recursiveCoupler(coupler.java:22)
    at commentCoupler.coupler.recursiveCoupler(coupler.java:22)
    at commentCoupler.main.main(main.java:16)

Edit: The exception also occurs when I do

comment = comment.replaceAll("**/", "");

and

comment = comment.replaceAll("*/", "");

Do anyone know why this happens and do anyone have a workaround?

Fruitdrops13
  • 21
  • 1
  • 7

4 Answers4

6

replaceAll takes a regular expression, and /** isn't a valid regular expression.

If you just want to remove /**, use replace instead.

aioobe
  • 413,195
  • 112
  • 811
  • 826
2

First parameter of "replaceAll" is a regular expression, and character "*" has a special meaning in regular expressions.

You can use something like this:

String a = "/**hola mundo/**, adios mundo";
String output = a.replaceAll("/\\*\\*", "");
System.out.println(output); // --> "hola mundo, adios mundo"
srcarro
  • 61
  • 3
0
comment = comment.replaceAll("/\**", "");
Dagriel
  • 574
  • 2
  • 12
  • Eclipse answers: "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )" – Fruitdrops13 May 12 '15 at 13:57
  • 1
    Not only is this regex but also a java string, \ must be escaped for java String level and * must be escaped for regex level. `/\\*\\*` is correct. – Captain Man May 12 '15 at 14:20
  • 1
    Besides the comments here, it is always preferable if you also explain your solution. If anything, it lets you think through your answer. – Magnilex May 12 '15 at 19:55
0

Two things are wrong with this code:

comment = comment.replaceAll("/**", "");
  1. * is a special regex character to match any number of any characters, based on the context that you are removing comments you want to match the literal * so you need your regex to be /\*\*.
  2. Because this regex is inside a java String it will try to read \* as an escape code (like \n being newline character), but \* isn't one. It needs to be \\* instead, meaning the String should be /\\*\\*

This is correct.

comment = comment.replaceAll("/\\*\\*", "");

As an aside, even if \* was a valid escape code, the thing it got replaced with would be passed into regex, not \*. (e.g., if you put \n in the newline character would go into regex, not a backslash and a lowercase n.


You may want to go with something like this though

comment = comment.replaceAll("/\\**\\*/", "");

which would be removing /* <stuff> */. This catches doc-comments and regular multi-line comments in addition to removing the entire comment. It depends on how you've read the string though.

Captain Man
  • 6,997
  • 6
  • 48
  • 74