Android about urls, phone numbers, regular expressions for mailboxes-most authoritative

Source: Internet
Author: User
Tags deprecated rfc tld
<span id="Label3"></p>Requirements: determine if the URL is legitimate<p><p>Today when writing a project, you need to be able to identify the function of the url, the first thought is the regular expression</p></p><p><p>But the types of Web sites are diverse, the network of various expressions also a lot of search, it is difficult to know which one of the great God wrote the reliable</p></p>Discovery: Textview's Android:autolink Property<p><p>today, the first thing to discover is that Android comes with a similar function inside the TextView function</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"> <textview android:id= "@+id/tv_scan_result" android:layout_width= "match_parent" android:layout_ height= "wrap_content" android:autolink= "web|email|phone|map" android:visibility= "gone"/></pre></pre><p><p>When the Autolink property is set, TextView will display the content according to the listed attribute values, special display of the url, mailbox, phone, and the event handling when Clicked.</p></p><p><p>so, textview, How did this come true?</p></p>Android.util.Patterns<p><p>Looking at a little bit of source discovery, Android provides an SDK that contains a class: android.util.Patterns;</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">/** * Regular expression pattern to match the most part of the RFC 3987 * Inter Nationalized URLs, aka IRIs. Commonly used Unicode characters is * added. */public static final Pattern Web_url = Pattern.compile ("(?:( http|https| http| https|rtsp| Rtsp): \\/\\/(?:(?: [a-za-z0-9\\$\\-\\_\\.\\+\\!\\*\\ ' \ \ \ "+" \\,\\;\\?\\&\\=]| (?: \ \%[a-fa-f0-9]{2})) {1,64} (?: \ \:(?: [a-za-z0-9\\$\\-\\_ "+" \\.\\+\\!\\*\\ ' \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\,\\;\\?\\&\\=]| (?:\ \%[a-fa-f0-9]{2})) {1,25})? \\@)? " + "(?:" + domain_name + ")" + "(?: \ \:\\d{1,5}) " Plus option port number + "(\\/(?:(?: [" + good_iri_char + "\\;\\/\\?\\:\\@\\&\\=\\#\\~"//plus option Quer Y params + "\\-\\.\\+\\!\\*\\" \ \ \ \\,\\_]) | (?:\ \%[a-fa-f0-9]{2}))? " + "(?:\ \b|$) "); and finally, a word boundary or end Of//input. This was to the stop foo.sure from//matching as foo.su </pre></pre>Working with instances<p><p>My own side of the method is as Follows:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"> /** * Determine if it is a valid URL address * @param url * @return * /private boolean isvalidurl (String url) { return Textutils.isempty (url) = = False && url.matches (Patterns.WEB_URL.pattern ()); }</pre></pre><p><p>There are many other patterns, such as mobile phone number, mail, IP address. The following adds the entire class in</p></p>The entire source<pre class="brush:java;collapse:true;;gutter:true;">/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR Implied. * See the License for the specific language governing permissions and * Limitations under the License. */package android.util;import java.util.regex.matcher;import java.util.regex.pattern;/** * Commonly used regular Expression Patterns. */public class Patterns {/** * Regular expression to match all IANA top-level domains. * List accurate as of 2011/07/18. List taken from: * Http://data.iana.org/TLD/tlds-alpha-by-domain.txt * This pattern is auto-generated by Framewo rks/ex/common/tools/make-iana-tld-pattern.py * * @deprecated Due to the recent profileration of gTLDs, this API is * Expected to become out-of-date very quickly. Therefore It is now * deprecated. */@Deprecated public static final String top_level_domain_str = "((aero|arpa|asia|a[cdefgilmnoqrstuwxz))" + "| (biz|b[abdefghijmnorstvwyz]) "+" | (cat|com|coop|c[acdfghiklmnoruvxyz]) "+" |d[ejkmoz] "+" | (edu|e[cegrstu]) "+" |f[ijkmor] "+" | (gov|g[abdefghilmnpqrstuwy]) "+" |h[kmnrtu] "+" | (info|int|i[delmnoqrst]) "+" | (jobs|j[emop]) "+" |k[eghimnprwyz] "+" |l[abcikrstuvy "+" | (mil|mobi|museum|m[acdeghklmnopqrstuvwxyz]) "+" | (name|net|n[acefgilopruz]) "+" | (org|om) "+" | (pro|p[aefghklmnrstwy]) "+" |qa "+" |r[eosuw "" + "|s[abcdeghijklmnortuvyz]" + "| (tel|travel|t[cdfghjklmnoprtvwz]) "+" |u[agksyz] "+" |v[aceginu] "+ "|w[fs" + "| (\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\ u0441\u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628 \u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\ u0633\u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a| \u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\ U0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e|\u092d\ U093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\u0ba8\ U0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\ u0baa\u0bc2\u0bb0\u0bcd|\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0dbd\u0d82 \u0d9a\U0dcf|\u0e44\u0e17\u0e22|\u30c6\u30b9\u30c8|\u4e2d\u56fd|\u4e2d\u570b|\u53f0\u6e7e|\u53f0\u7063|\u65b0\u52a0\ u5761|\u6d4b\u8bd5|\u6e2c\u8a66|\u9999\u6e2f|\ud14c\uc2a4\ud2b8|\ud55c\uad6d|xn\\-\\-0zwm56d|xn\\-\\- 11b5bs3a9aj6g|xn\\-\\-3e0b707e|xn\\-\\-45brj9c|xn\\-\\-80akhbyknj4f|xn\\-\\-90a3ac|xn\\-\\-9t4b11yi5a|xn\\-\\- clchc0ea0b2g2a9gcd|xn\\-\\-deba0ad|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\- g6w251d|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-j6w193g|xn\\-\\- jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgbaam7a8h|xn\\-\\- Mgbayh7gpa|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgberp4a5d4ar|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\ \-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-s9brj9c|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xkc2al3hye2a|xn\\-\\- Xkc2dl3a5ee0h|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zckzah|xxx) "+" |y[et] "+" |z[amw]) "; /** * Regular expression PatteRN to match all IANA top-level domains. * @deprecated this API is Deprecated. See {@link #TOP_LEVEL_DOMAIN_STR}. */@Deprecated public static final Pattern Top_level_domain = Pattern.compile (top_level_domain_str); /** * Regular expression to match all IANA top-level domains for web_url. * List accurate as of 2011/07/18. List taken from: * Http://data.iana.org/TLD/tlds-alpha-by-domain.txt * This pattern is auto-generated by Framewo rks/ex/common/tools/make-iana-tld-pattern.py * * @deprecated this API is Deprecated. See {@link #TOP_LEVEL_DOMAIN_STR}. */@Deprecated public static final String Top_level_domain_str_for_web_url = "(?:" + "(?: Aero|arpa|asi A|a[cdefgilmnoqrstuwxz]) "+" | (?: Biz|b[abdefghijmnorstvwyz]) "+" | (?: cat|com|coop|c[acdfghiklmnoruvxyz]) "+" |d[ejkmoz] "+" | (?: edu|e[cegrstu]) "+" |f[ijkmor] "+" | (?: gov|g[abdefghilmnpqrstuwy]) "+" |h[kmnrtu] " + "| (?: Info|int|i[delmnoqrst]) "+" | (?: jobs|j[emop]) "+" |k[eghimnprwyz] "+" |l[abcikrstuvy] "+" | (?: Mil|mobi|museum|m[acdeghklmnopqrstuvwxyz]) "+" | (?: Name|net|n[acefgilopruz]) "+" | (?: o Rg|om) "+" | (?:p ro|p[aefghklmnrstwy]) "+" |qa "+" |r[eosuw] "+" |s[abcdeghijklmnortuvyz] "+" | (?: tel|travel|t[cdfghjklmnoprtvwz]) "+" |u[agksyz] "+" |v[aceginu] "+" |w[fs] "+" | (?:\ u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\u0441 \u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628\ u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633 \u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a|\ U0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e |\u092d\u093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\ U0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2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ckzah|xxx) "+" |y[et] "+" |z[amw]) "; /** * Good characters for internationalized Resource Identifiers (IRI). * This comprises most common used Unicode characters allowed in IRI * as detailed in RFC 3987. * specifically, Those-byte Unicode characters is not included. */public static final String Good_iri_char = "a-za-z0-9\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef"; public static final Pattern ip_address = Pattern.compile ("((25[0-5]|2[0-4][0-9]|[ 0-1][0-9]{2}| [1-9] [0-9]| [1-9]) \\. (25[0-5]|2[0-4] "+" [0-9]| [0-1] [0-9] {2}| [1-9] [0-9]| [1-9]|0] \ \. (25[0-5]|2[0-4][0-9]| [0-1] "+" [0-9]{2}| [1-9] [0-9]| [1-9]|0] \ \. (25[0-5]|2[0-4][0-9]| [0-1] [0-9] {2} "+" | [1-9] [0-9]| [0-9])); /** * RFC 1035 Section 2.3.4 limits the labels to a maximum Octets. */private static final String IRI = "[" + Good_iri_char + "] ([" + Good_iri_char + "\\-]{0,61}[" + Good_iri_char + "]) {0,1}"; private static final String Good_gtld_char = "a-za-z\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef"; private static final String GTLD = "[" + Good_gtld_char + "]{2,63}"; private static final String host_name = "(" + IRI + "\ \.) + "+ GTLD; public static final Pattern domain_name = pattern.compile ("(" + host_name + "|" + ip_address + ")"); /** * Regular expression pattern to match the most part of the RFC 3987 * internationalized URLs, aka IRIs. Commonly used Unicode characters is * added. */public static final Pattern Web_url = Pattern.compile ("(?:( http|https| http| https|rtsp| Rtsp): \\/\\/(?:(?: [a-za-z0-9\\$\\-\\_\\.\\+\\!\\*\\ ' \ \ \ ' + ' \\,\\;\\?\\&\\=]| (?:\ \%[a-fa-f0-9]{2})) {1,64} (?: \ \:(?: [a-za-z0-9\\$\\-\\_ "+" \\.\\+\\!\\*\\ ' \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\,\\;\\?\\&\\=]| (?:\ \%[a-fa-f0-9]{2})) {1,25})? \\@)? " + "(?:" + domain_name + ")" + "(?: \ \:\\d{1,5}) " Plus option port number + "(\\/(?:(?: [" + good_iri_char + "\\;\\/\\?\\:\\@\\&\\=\\#\\~"//plus option Quer Y params + "\\-\\.\\+\\!\\*\\" \ \ \ \\,\\_]) | (?:\ \%[a-fa-f0-9]{2}))? " + "(?:\ \b|$) "); and finally, a word boundary or end Of//input. This was to the stop Foo.sure from//matching as foo.su public static final Pattern email_address = Pattern.compile ("[a-za-z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-za-z0-9][a-z a-z0-9\\-]{0,64} "+" ("+" \ \ "+" [a-za-z0-9][a-za-z0-9\\-]{0,25} "+ ")+" ); /** * This pattern is IntenDed for searching for things it look like they * might is phone numbers in arbitrary text, not for validating whethe R * Something is in fact a phone number. It'll Miss many things that * is legitimate phone numbers. * <p> The pattern matches the following: * <ul> * <li>optionally, A + sign followed Immedi Ately by one or more digits. Spaces, dots, or dashes * may follow. * <li>optionally, sets of digits in parentheses, separated by spaces, dots, or Dashes. * <li>a string starting and ending with A digit, containing digits, spaces, dots, and/or Dashes. * </ul> */public static final Pattern PHONE = pattern.compile (//SDD = space, D ot, or Dash "(\\+[0-9]+[\\-\.) *)?" +<digits><sdd>* + "(\ \ ([0-9]+\\) [\\-\ \.] *)?" (<digits>) <sdd>* + "([0-9][0-9\\-\ \.] +[0-9]); <digit><digit|sdd>+<digit>/** * Convenience method to take any of the non-null matching groups in a * regex Ma Tcher and return them as a concatenated string. * * @param matcher the Matcher object from which grouped text would * be extracted * * @return A String comprising all of the non-null matched * groups concatenated Together */public static final String concatgroups (Matcher Matcher) {StringBuilder b = new StringBuilder ( ); Final int numgroups = Matcher.groupcount (); for (int i = 1; i <= numgroups; i++) {String s = Matcher.group (i); If (s! = Null) {b.append (s); }} return b.tostring (); }/** * Convenience method to return only the digits and plus signs * in the matching string. * * @param matcher the Matcher object from which digits and plus would * Be extracted * * @return A String comprising all of the digits and plus in * The match */public static final String digitsandplusonly (Matcher Matcher) {StringBuilder buffer = new StringBuilder (); String matchingregion = Matcher.group (); for (int i = 0, size = matchingregion.length (), i < size; i++) {char character = Matchingregion.charat (i); if (character = = ' + ' | | Character.isdigit (Character)) {buffer.append (Character); }} return buffer.tostring (); }/** * Do not create the this static utility class. */private Patterns () {}}</pre><p><p>  </p></p><p><p>Android about urls, phone numbers, regular expressions for mailboxes-most authoritative</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.