この正規表現を使用してみてください (1 行のコメントのみ):
String src ="How are things today /* this is comment */ and is your code /* this is another comment */ working?";
String result=src.replaceAll("/\\*.*?\\*/","");//single line comments
System.out.println(result);
REGEX の説明:
文字「/」を文字通り一致させる
文字「*」に文字通り一致
「。」任意の 1 文字に一致
「*?」ゼロ回から無制限の回数、できるだけ少ない回数、必要に応じて拡張 (遅延)
文字「*」に文字通り一致
文字「/」を文字通り一致させる
別の方法として、 (?s)を追加することによる単一行および複数行のコメントの正規表現を次に示します。
//note the added \n which wont work with previous regex
String src ="How are things today /* this\n is comment */ and is your code /* this is another comment */ working?";
String result=src.replaceAll("(?s)/\\*.*?\\*/","");
System.out.println(result);
参照: