如何从Android上的String获取URL

You*_*Cha 0 java string url android android-edittext

我想从中提取URL hi there this is a URL String http://mytest.com.

我尝试使用EditText.getURLs,但它对我不起作用.

EditText.setText("hi there this is a URL String http://stackoverflow.com");
EditText.getURLs.toString();
Run Code Online (Sandbox Code Playgroud)

如何从EditText获取URL?

And*_*rei 12

这是功能:

//Pull all links from the body for easy retrieval
private ArrayList pullLinks(String text) {
   ArrayList links = new ArrayList();

   String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
   Pattern p = Pattern.compile(regex);
   Matcher m = p.matcher(text);
   while(m.find()) {
      String urlStr = m.group();
      if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
         urlStr = urlStr.substring(1, urlStr.length() - 1);
      }
      links.add(urlStr);
   }
   return links;
}
Run Code Online (Sandbox Code Playgroud)