尝试使用此正则表达式(仅单行注释):
String src ="How are things today and is your pre working?";String result=src.replaceAll("/\*.*?\*/","");//single line commentsSystem.out.println(result);REGEX解释:
从字面上匹配字符
“ /”
从字面上匹配字符
“ *”
“。”匹配任何单个字符
“ *?”在0到无限制的时间之间,尽可能少的时间,根据需要扩展(延迟)
从字面上匹配字符
“ *”
从字面上匹配字符
“ /”
另外,这里是通过添加(?s)来表示单行和多行注释的正则表达式:
//note the added n which wont work with previous regexString src ="How are things today and is your pre working?";String result=src.replaceAll("(?s)/\*.*?\*/","");System.out.println(result);


