Pattern.compile("((http\\://|https\\://|ftp\\://|sftp\\://)|(www.))+((\\S+):(\\S+)@)?+(([a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(/[a-zA-Z0-9%:/-_\\?\\.'~]*)?");
我有这个模式,我想测试我的字符串中是否有链接。
我想在 TextView
中链接这些文本。
当链接包含 &
字符时,代码不起作用。
完整代码:
Pattern httpMatcher = Pattern.compile("((http\\://|https\\://|ftp\\://|sftp\\://)|(www.))+((\\S+):(\\S+)@)?+(([a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(/[a-zA-Z0-9%:/-_\\?\\.'~]*)?");
String httpViewURL = "myhttp://";
Linkify.addLinks(label, httpMatcher, httpViewURL);
最佳答案
我认为这比使用正则表达式更干净:
boolean isLink(String s) {
try {
new URL(s);
return true;
} catch (MalformedURLException e) {
return false;
}
}
关于Java/Android 正则表达式测试字符串中是否为链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14326625/