|
| 1 | +package com.termux.shared.data; |
| 2 | + |
| 3 | +import java.util.LinkedHashSet; |
| 4 | +import java.util.regex.Matcher; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +public class UrlUtils { |
| 8 | + |
| 9 | + public static Pattern URL_MATCH_REGEX; |
| 10 | + |
| 11 | + public static Pattern getUrlMatchRegex() { |
| 12 | + if (URL_MATCH_REGEX != null) return URL_MATCH_REGEX; |
| 13 | + |
| 14 | + StringBuilder regex_sb = new StringBuilder(); |
| 15 | + |
| 16 | + regex_sb.append("("); // Begin first matching group. |
| 17 | + regex_sb.append("(?:"); // Begin scheme group. |
| 18 | + regex_sb.append("dav|"); // The DAV proto. |
| 19 | + regex_sb.append("dict|"); // The DICT proto. |
| 20 | + regex_sb.append("dns|"); // The DNS proto. |
| 21 | + regex_sb.append("file|"); // File path. |
| 22 | + regex_sb.append("finger|"); // The Finger proto. |
| 23 | + regex_sb.append("ftp(?:s?)|"); // The FTP proto. |
| 24 | + regex_sb.append("git|"); // The Git proto. |
| 25 | + regex_sb.append("gopher|"); // The Gopher proto. |
| 26 | + regex_sb.append("http(?:s?)|"); // The HTTP proto. |
| 27 | + regex_sb.append("imap(?:s?)|"); // The IMAP proto. |
| 28 | + regex_sb.append("irc(?:[6s]?)|"); // The IRC proto. |
| 29 | + regex_sb.append("ip[fn]s|"); // The IPFS proto. |
| 30 | + regex_sb.append("ldap(?:s?)|"); // The LDAP proto. |
| 31 | + regex_sb.append("pop3(?:s?)|"); // The POP3 proto. |
| 32 | + regex_sb.append("redis(?:s?)|"); // The Redis proto. |
| 33 | + regex_sb.append("rsync|"); // The Rsync proto. |
| 34 | + regex_sb.append("rtsp(?:[su]?)|"); // The RTSP proto. |
| 35 | + regex_sb.append("sftp|"); // The SFTP proto. |
| 36 | + regex_sb.append("smb(?:s?)|"); // The SAMBA proto. |
| 37 | + regex_sb.append("smtp(?:s?)|"); // The SMTP proto. |
| 38 | + regex_sb.append("svn(?:(?:\\+ssh)?)|"); // The Subversion proto. |
| 39 | + regex_sb.append("tcp|"); // The TCP proto. |
| 40 | + regex_sb.append("telnet|"); // The Telnet proto. |
| 41 | + regex_sb.append("tftp|"); // The TFTP proto. |
| 42 | + regex_sb.append("udp|"); // The UDP proto. |
| 43 | + regex_sb.append("vnc|"); // The VNC proto. |
| 44 | + regex_sb.append("ws(?:s?)"); // The Websocket proto. |
| 45 | + regex_sb.append(")://"); // End scheme group. |
| 46 | + regex_sb.append(")"); // End first matching group. |
| 47 | + |
| 48 | + |
| 49 | + // Begin second matching group. |
| 50 | + regex_sb.append("("); |
| 51 | + |
| 52 | + // User name and/or password in format 'user:pass@'. |
| 53 | + regex_sb.append("(?:\\S+(?::\\S*)?@)?"); |
| 54 | + |
| 55 | + // Begin host group. |
| 56 | + regex_sb.append("(?:"); |
| 57 | + |
| 58 | + // IP address (from http://www.regular-expressions.info/examples.html). |
| 59 | + regex_sb.append("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|"); |
| 60 | + |
| 61 | + // Host name or domain. |
| 62 | + regex_sb.append("(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))?|"); |
| 63 | + |
| 64 | + // Just path. Used in case of 'file://' scheme. |
| 65 | + regex_sb.append("/(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)"); |
| 66 | + |
| 67 | + // End host group. |
| 68 | + regex_sb.append(")"); |
| 69 | + |
| 70 | + // Port number. |
| 71 | + regex_sb.append("(?::\\d{1,5})?"); |
| 72 | + |
| 73 | + // Resource path with optional query string. |
| 74 | + regex_sb.append("(?:/[a-zA-Z0-9:@%\\-._~!$&()*+,;=?/]*)?"); |
| 75 | + |
| 76 | + // Fragment. |
| 77 | + regex_sb.append("(?:#[a-zA-Z0-9:@%\\-._~!$&()*+,;=?/]*)?"); |
| 78 | + |
| 79 | + // End second matching group. |
| 80 | + regex_sb.append(")"); |
| 81 | + |
| 82 | + URL_MATCH_REGEX = Pattern.compile( |
| 83 | + regex_sb.toString(), |
| 84 | + Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); |
| 85 | + |
| 86 | + return URL_MATCH_REGEX; |
| 87 | + } |
| 88 | + |
| 89 | + public static LinkedHashSet<CharSequence> extractUrls(String text) { |
| 90 | + LinkedHashSet<CharSequence> urlSet = new LinkedHashSet<>(); |
| 91 | + Matcher matcher = getUrlMatchRegex().matcher(text); |
| 92 | + |
| 93 | + while (matcher.find()) { |
| 94 | + int matchStart = matcher.start(1); |
| 95 | + int matchEnd = matcher.end(); |
| 96 | + String url = text.substring(matchStart, matchEnd); |
| 97 | + urlSet.add(url); |
| 98 | + } |
| 99 | + |
| 100 | + return urlSet; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments