简体   繁体   中英

Java how can remove everything between two substring in a string

I want to remove any substring(s) in a string that begins with 'galery' and ends with 'jssdk));'

For instance, consider the following string:
Galery something something.... jssdk));

I need an algorithm that removes 'something something....' and returns 'Galery jssdk));'

This is what I've done, but it does not work.

newsValues[1].replaceAll("Galery.*?jssdK));", "");

Could probably be improved, I've done it fast:

  public static String replaceMatching(String input, String lowerBound, String upperBound{
    Pattern p = Pattern.compile(".*?"+lowerBound+"(.*?)"+upperBound+".*?");
    Matcher m = p.matcher(input);
    String textToRemove = "";

    while(m.find()){
        textToRemove = m.group(1);
    }
    return input.replace(textToRemove, "");
}

UPDATE Thx for accepting the answer, but here is a smaller reviewed version:

 public static String replaceMatching2(String input, String lowerBound, String upperBound){
      String result = input.replaceAll("(.*?"+lowerBound + ")" + "(.*?)" + "(" + upperBound + ".*)", "$1$3");
      return result;
 }

The idea is pretty simple actually, split the String into 3 groups, and replace those 3 groups with the first and third, droping the second one.

You are almost there, but that will remove the entire string. If you want to remove anything between Galery and jssdK)); , you will have to do something like so:

String newStr = newsValues[1].replaceAll("(Galery)(.*?)(jssdK\\\\)\\\\);)","$1$3");

This will put the strings into groups and will then use these groups to replace the entire string. Note that in regex syntax, the ) is a special character so it needs to be escaped.

String str = "GaleryABCDEFGjssdK));";
String newStr = str.replaceAll("(Galery)(.*?)(jssdK\\)\\);)","$1$3");
System.out.println(newStr);

This yields: GaleryjssdK));

I know that the solution presented by @amit is simpler, however, I thought it would be a good idea to show you a useful way in which you can use the replaceAll method.

Simplest solution will be to replace the string with just the "edges", effectively "removing" 1 everything between them.

newsValues[1].replaceAll("Galery.*?jssdK));", "GaleryjssdK));");

1: I used "" here because it is not exactly replacing - remember strings are immutable, so it is creating a new object, without the "removed" part.

newsValues[1] = newsValues[1].substring(0,6)+newsValues.substring(newsValues[1].length()-5,newsValues[1].length())

This basically concatenates the "Galery" and the "jssdk" leaving or ignoring everything else. More importantantly, you can simply assign newValues[1] = "Galeryjssdk"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM