Solved Replace All not Working.

Discussion in 'Plugin Development' started by BlueMustache, Aug 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    BlueMustache

    Hey Guys,
    Ive been working on a little trickery with strings.
    I ran into a problem thought.
    I have a string that says "1.7.10)", how can I get rid of that ")"?
    This is what I tried.

    Code:
        String tempVersion = getServer().getVersion();
        String[] version = tempVersion.split(" ");
        String finalversion = version[2];
        finalversion.replaceAll("\\(.+\\)", "");
        downloadAssets(finalversion);
    Any ideas what to do instead?
    Please help,
    -Blue
     
  2. Offline

    Jake6177

    Well if you don't need to use the regex for other things...how about finalversion = finalversion.substring(0, finalstring.length()-1)?
     
    BlueMustache likes this.
  3. Offline

    stormneo7

    .replaceAll(")", "");
     
  4. Offline

    BlueMustache

    Jake6177
    I'll try that, thanks!

    stormneo7
    Tried that before, shoots out a huge error.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  5. Offline

    RainoBoy97

    BlueMustache
    Try .replace(']', ' '); since you're only replacing one character.
     
  6. Offline

    stormneo7

    Then try .replaceAll("\)", "");
    Edit nvm
    Code:
    Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')'
    )
        at java.util.regex.Pattern.error(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
        at java.util.regex.Pattern.<init>(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
        at java.lang.String.replaceAll(Unknown Source)
        at Main.main(Main.java:191)
    
    Use jake's code.
    [​IMG]
     
  7. Offline

    Zupsub

    Use String#replace() to replace ALL matches, without regex. (String.replace("(", ""))
    Use String#replaceAll to replace ALL matches, with regex:
    Since ) is a special regex character, we have to use \) (with a backslash)
    But since \ is a special character in an String, we have to escape it, too: \\)
    So all in all String#replaceAll("\\)", "");
     
    Jake6177 likes this.
Thread Status:
Not open for further replies.

Share This Page