【问题标题】:Remove all characters between 2 characters in a String删除字符串中 2 个字符之间的所有字符
【发布时间】:2015-07-23 17:25:21
【问题描述】:

我有一个包含 50000 行的数据库。我想一一获取所有行,将它们放入字符串变量并删除“(”和“)”之间的所有字符,然后更新该行。我只是不知道如何删除“(”和“)”之间的所有字符。我想用java来做这个。

【问题讨论】:

标签: java string char between removeall


【解决方案1】:

你的正则表达式应该是这样的:

data.replaceAll("\\(.*?\\)", "()"); //if you want to keep the brackets

data.replaceAll("\\(.*?\\)", ""); //if you don't want to keep the brackets

字符串由三部分组成:

\\(  //This part matches the opening bracket
.*?  //This part matches anything in between
\\)  //This part matches the closing bracket

希望对你有帮助

【讨论】:

    【解决方案2】:

    使用replaceall(string regex, string replacement) 语句,如下所示:

    data.replaceall(" and ", "");
    

    【讨论】:

    • 可以把字符串的内容复制到这里吗?
    • String temp = "Bold (medium) ...";我需要删除(中等)并使我的字符串像这样“Bold ...”
    • 尝试使用 temp.replace("(medium)","");
    • 我告诉我有 50000 个不同的字符串,我需要删除该字符串中“(”和“)”之间的每个字符。我只需要一个删除两个特殊字符之间所有字符的代码。
    • @Matarata 您需要在正则表达式评估之前转义java代码中的反斜杠:s.replaceAll("\\(.*?\\)", "()");
    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多