Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除字符串中引号之间的注释_Java_String - Fatal编程技术网

Java 删除字符串中引号之间的注释

Java 删除字符串中引号之间的注释,java,string,Java,String,我需要删除字符串中的注释。注释使用引号指定。例如: removeComments("1+4 'sum'").equals("1+4 "); removeComments("this 'is not a comment").equals("this 'is not a comment"); removeComments("1+4 'sum' 'unclosed comment").equals("1+4 'unclosed comment"); 我可以遍历字符串的字符,跟踪引号的索引,但我想知

我需要删除字符串中的注释。注释使用引号指定。例如:

removeComments("1+4 'sum'").equals("1+4 ");
removeComments("this 'is not a comment").equals("this 'is not a comment");
removeComments("1+4 'sum' 'unclosed comment").equals("1+4  'unclosed comment");
我可以遍历字符串的字符,跟踪引号的索引,但我想知道是否有更简单的解决方案(可能是正则表达式?)

您可以使用:

str=str.replaceAll(“\\'.\”,”)

这将用
”替换第一个和第二个
,以及它们之间的所有内容(因此,将删除它们)



编辑:如注释中所述,不需要反斜杠将单个引号括起来。

如果不需要在注释中包含引号,则可以这样做:

input.replaceAll("'[^']+'", "");
它匹配一个引号,至少一个不是引号的东西,然后是引号


是否允许使用转义机制在评论中包含单引号?@Nick:这是不允许的。该字段实际上是一个公式字段,将对其进行计算。和“在公式中没有意义,所以不需要有转义机制。你不需要用反斜杠转义单引号。@尼克,你说得对。我在写了答案后就想出来了(我会编辑它)。