3

I have the following string:

String description= errex for Screen Share 
https://ednovo.webex.com/ednovo/j.php?ED=224466857&UID=1465621292&RT=MiM0
You can find the meeting notes here https://docs.yahoo.com/a/filter.org/document/d/1Luf_6Q73_Lm30t3x6wHS_4Ztkn7HfXDg4sZZWz-CuVw/edit?usp=sharing

I want to remove the url link and end up with this:

String description=errex for Screen Share You can find the meeting notes here 

I tried the following code but it is not detecting the URL:

private String removeUrl(String commentstr)
    {
        String commentstr1=commentstr;
        String urlPattern = "((https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
        Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(commentstr1);
        int i=0;
        while (m.find()) {
            commentstr1=commentstr1.replaceAll(m.group(i),"").trim();
            i++;
        }
        System.out.println("After url filter" +commentstr1);

        return commentstr1;
    }

What is wrong here?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
user2841300
  • 353
  • 1
  • 7
  • 23
  • if you're sure of the fact that url can't have spaces you could easily split string. Then you could parse all the resulting array with a regex or simply catching (and ignoring) the string wich contains special chars. If it's ok for you I could arrange a simple class. – Black.Jack Nov 22 '13 at 10:56
  • Look at this SO question http://stackoverflow.com/questions/12366496/removing-the-url-from-text-using-java – Alexey Odintsov Nov 22 '13 at 10:58

2 Answers2

8

This will remove urls:

description = description.replaceAll("https?://\\S+\\s?", "");

Btw the little \\s? at the end ensures you don't get double spaces after the URL has been removed from between two spaces.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
3
String description= "errex for Screen Share https://ednovo.webex.com/ednovo/j.php?ED=224466857&UID=1465621292&RT=MiM0 " +
    "You can find the meeting notes here https://docs.yahoo.com/a/filter.org/document/d/1Luf_6Q73_Lm30t3x6wHS_4Ztkn7HfXDg4sZZWz-CuVw/edit?usp=sharing";

System.out.println(description.replaceAll("\\S+://\\S+", ""));
Bohemian
  • 412,405
  • 93
  • 575
  • 722
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55