匹配C樣式多行注釋的Regex
3 回答

縹緲止盈
TA貢獻2041條經(jīng)驗 獲得超4個贊
String src ="How are things today /* this is comment */ and is your code /* this is another comment */ working?";String result=src.replaceAll("/\\*.*?\\*/","");//single line commentsSystem.out.println(result);
REGEX解釋說:
匹配字符“/”字面上
匹配字符“*”字面意思
“.”匹配任何單個字符
“*?”在零和無限倍之間,盡可能少,根據(jù)需要擴展(懶惰)
匹配字符“*”字面意思
匹配字符“/”字面上
//note the added \n which wont work with previous regexString 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);
參考資料:
- 3 回答
- 0 關注
- 477 瀏覽
添加回答
舉報
0/150
提交
取消