-2

Origin string :

String str = "others. https://forum.com/thread.jspa?thread$ID=251290(tr)start=75&t-start=0 I recently"

Target String:

"others. I recently";

I applied the 11 votes answer of - Removing the url from text using java but it didn't work on my url.

Thanks for help.

shinyshinx
  • 53
  • 5

1 Answers1

0

I. If you have always the struture : word url word (and url without spaces) you can use :

String str = "others. https://forum.com/thread.jspa[...]art=75&t-start=0 I recently";
Matcher m = Pattern.compile("(.*)http.*\\s(.*)").matcher(str); // (capture1)url (capture2)
String target = "";
if (m.find()) {
    target = m.group(1) + m.group(2);
}
System.out.println(target);  //others. recently

The pattern will capture 2 groups, the one before the url, and the one after the space after the url


II. Iterate over the words of the sentence and keep the ones which does not contains "http" which significate that it's an url

StringBuilder builder = new StringBuilder(); 
for (String word : str.split(" ")) { 
     if (!word.contains("http")) { 
         builder.append(word).append(" "); 
     } 
} 
String target = builder.deleteCharAt(builder.length()-1).toString();
System.out.println(target); //others. recently

III. Same as II. but wih streams (ONE LINE solution) :

String target = Arrays.asList(str.split(" "))
                      .stream()
                      .filter(word -> !word.contains("http"))
                      .map(word -> word + " ")
                      .collect(Collectors.joining());
azro
  • 53,056
  • 7
  • 34
  • 70