Search code examples
javastringcharbetweenremoveall

Remove all characters between 2 characters in a String


I have a database with 50000 rows. I want to get all rows one by one, put them into a String variable and delete all characters between "(" and ")", then update the row. I just don't know how to delete all characters between "(" and ")". I want to do this with java.


Solution

  • Your regular expression would be something like:

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

    OR

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

    The string consists of 3 parts:

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

    I hope this helps