-3

我正在开发一个 android 应用程序,我想识别 EditText 内的链接。我有下一个在objective-c中工作的代码,我想用java来做。我想这将与向量(字符串链接 [])有关,但我不知道。

NSError *qwerror = nil;
NSRegularExpression *qwregex = [NSRegularExpression regularExpressionWithPattern:@"(http://\\w+.\\w+.\\w+.\\w+.\\w+.)" options:0 error:&qwerror];
NSArray *qwmatches = [qwregex matchesInString:textView.text options:0 range:NSMakeRange(0, textView.text.length)];
NSMutableArray *qwwords = [NSMutableArray array];
for (NSTextCheckingResult *qwmatch in qwmatches) {
    NSRange qwwordrange = [qwmatch rangeAtIndex:1];
    NSString *qwword = [textView.text substringWithRange:qwwordrange];
    [qwwords addObject:qwword];
}
NSString *allLinks = [qwwords componentsJoinedByString:@" "];
4

1 回答 1

0

这可能有效:

import java.net.URL;
import java.util.List;

String input = /* text from edit text */;

String[] words = input.split("\\s");
List<URL> urls;

for (String s : words)
{
    try 
    {
        urls.add(new URL(s));
    }
    catch (MalformedURLException e)
    {
        // not a url
    }
}

// urls contains all urls from 'input'.
于 2012-05-12T19:03:55.343 回答