2

から 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();

EditText から URL を取得するにはどうすればよいですか?

4

3 に答える 3

14

関数は次のとおりです。

//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;
}
于 2013-09-05T07:43:27.873 に答える