gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:00:35 26 4
gpt4 key购买 nike

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

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");

我可以迭代字符串的字符,跟踪引号的索引,但我想知道是否有更简单的解决方案(也许是正则表达式?)

最佳答案

您可以使用replaceAll :

str = str.replaceAll("\\'.*?\\'", "");

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

<小时/>

编辑:如评论中所述,无需反斜杠单引号。

关于java - 删除字符串中引号之间的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16010594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com