簡體   English   中英

在Java代碼中檢測和刪除單行注釋

[英]detecting and removing single line comments in code in Java

因此,我正在制作此編譯器,它將文本文件編譯為用於OS的機器代碼以從SD卡讀取的機器代碼。 代碼如下所示:(Ra text_to_store)將文本存儲到字符串插槽a中。 大部分似乎是可行的

mystring=mystring.replace("String ","R");
mystring=mystring.replace("=","");
mystring=mystring.replace(";","");
//cannot replace " with nothing, how do I get around this?

因為這變成String a = text_t_ store; 輸入正確的代碼。

但是當然不能用這一切。 例如,我需要擺脫一些注釋(//已注釋的內容),而注釋和/或注釋內容不能與.replace一起使用。 這可能很簡單,但是我找不到它!

ps您還可以提供文本格式的鏈接/幫助嗎? 例如int var; 需要轉換為RAM插槽的數字。 提前致謝!

使用mystring.replaceAll,它使用正則表達式,例如

mystring = mystring.replaceAll("//.*$","");

從//替換到行尾,不進行任何操作

您可以使用replaceAll和一個正則表達式將所有內容放在一行中

mystring=mystring.replaceAll("(?:.|\\s)*String ([\\w]+)=(.*?);(?:.|\\s)*","R$1 $2");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM