繁体   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