1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util;
18 
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 
22 /**
23  * Commonly used regular expression patterns.
24  */
25 @android.ravenwood.annotation.RavenwoodKeepWholeClass
26 public class Patterns {
27     /**
28      *  Regular expression to match all IANA top-level domains.
29      *  List accurate as of 2011/07/18.  List taken from:
30      *  http://data.iana.org/TLD/tlds-alpha-by-domain.txt
31      *  This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
32      *
33      *  @deprecated Due to the recent profileration of gTLDs, this API is
34      *  expected to become out-of-date very quickly. Therefore it is now
35      *  deprecated.
36      */
37     @Deprecated
38     public static final String TOP_LEVEL_DOMAIN_STR =
39         "((aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
40         + "|(biz|b[abdefghijmnorstvwyz])"
41         + "|(cat|com|coop|c[acdfghiklmnoruvxyz])"
42         + "|d[ejkmoz]"
43         + "|(edu|e[cegrstu])"
44         + "|f[ijkmor]"
45         + "|(gov|g[abdefghilmnpqrstuwy])"
46         + "|h[kmnrtu]"
47         + "|(info|int|i[delmnoqrst])"
48         + "|(jobs|j[emop])"
49         + "|k[eghimnprwyz]"
50         + "|l[abcikrstuvy]"
51         + "|(mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
52         + "|(name|net|n[acefgilopruz])"
53         + "|(org|om)"
54         + "|(pro|p[aefghklmnrstwy])"
55         + "|qa"
56         + "|r[eosuw]"
57         + "|s[abcdeghijklmnortuvyz]"
58         + "|(tel|travel|t[cdfghjklmnoprtvwz])"
59         + "|u[agksyz]"
60         + "|v[aceginu]"
61         + "|w[fs]"
62         + "|(\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)"
63         + "|y[et]"
64         + "|z[amw])";
65 
66     /**
67      *  Regular expression pattern to match all IANA top-level domains.
68      *  @deprecated This API is deprecated. See {@link #TOP_LEVEL_DOMAIN_STR}.
69      */
70     @Deprecated
71     public static final Pattern TOP_LEVEL_DOMAIN =
72         Pattern.compile(TOP_LEVEL_DOMAIN_STR);
73 
74     /**
75      *  Regular expression to match all IANA top-level domains for WEB_URL.
76      *  List accurate as of 2011/07/18.  List taken from:
77      *  http://data.iana.org/TLD/tlds-alpha-by-domain.txt
78      *  This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
79      *
80      *  @deprecated This API is deprecated. See {@link #TOP_LEVEL_DOMAIN_STR}.
81      */
82     @Deprecated
83     public static final String TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL =
84         "(?:"
85         + "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
86         + "|(?:biz|b[abdefghijmnorstvwyz])"
87         + "|(?:cat|com|coop|c[acdfghiklmnoruvxyz])"
88         + "|d[ejkmoz]"
89         + "|(?:edu|e[cegrstu])"
90         + "|f[ijkmor]"
91         + "|(?:gov|g[abdefghilmnpqrstuwy])"
92         + "|h[kmnrtu]"
93         + "|(?:info|int|i[delmnoqrst])"
94         + "|(?:jobs|j[emop])"
95         + "|k[eghimnprwyz]"
96         + "|l[abcikrstuvy]"
97         + "|(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
98         + "|(?:name|net|n[acefgilopruz])"
99         + "|(?:org|om)"
100         + "|(?:pro|p[aefghklmnrstwy])"
101         + "|qa"
102         + "|r[eosuw]"
103         + "|s[abcdeghijklmnortuvyz]"
104         + "|(?:tel|travel|t[cdfghjklmnoprtvwz])"
105         + "|u[agksyz]"
106         + "|v[aceginu]"
107         + "|w[fs]"
108         + "|(?:\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)"
109         + "|y[et]"
110         + "|z[amw]))";
111 
112     /**
113      *  Regular expression to match all IANA top-level domains.
114      *
115      *  List accurate as of 2023/09/11.  List taken from:
116      *  http://data.iana.org/TLD/tlds-alpha-by-domain.txt
117      *  This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
118      *
119      *  @hide
120      */
121     static final String IANA_TOP_LEVEL_DOMAINS =
122         "(?:"
123         + "(?:aaa|aarp|abb|abbott|abbvie|abc|able|abogado|abudhabi|academy|accenture|accountant"
124         + "|accountants|aco|actor|ads|adult|aeg|aero|aetna|afl|africa|agakhan|agency|aig|airbus"
125         + "|airforce|airtel|akdn|alibaba|alipay|allfinanz|allstate|ally|alsace|alstom|amazon|americanexpress"
126         + "|americanfamily|amex|amfam|amica|amsterdam|analytics|android|anquan|anz|aol|apartments"
127         + "|app|apple|aquarelle|arab|aramco|archi|army|arpa|art|arte|asda|asia|associates|athleta"
128         + "|attorney|auction|audi|audible|audio|auspost|author|auto|autos|avianca|aws|axa|azure"
129         + "|a[cdefgilmoqrstuwxz])"
130         + "|(?:baby|baidu|banamex|bananarepublic|band|bank|bar|barcelona|barclaycard|barclays"
131         + "|barefoot|bargains|baseball|basketball|bauhaus|bayern|bbc|bbt|bbva|bcg|bcn|beats|beauty"
132         + "|beer|bentley|berlin|best|bestbuy|bet|bharti|bible|bid|bike|bing|bingo|bio|biz|black"
133         + "|blackfriday|blockbuster|blog|bloomberg|blue|bms|bmw|bnpparibas|boats|boehringer|bofa"
134         + "|bom|bond|boo|book|booking|bosch|bostik|boston|bot|boutique|box|bradesco|bridgestone"
135         + "|broadway|broker|brother|brussels|build|builders|business|buy|buzz|bzh|b[abdefghijmnorstvwyz])"
136         + "|(?:cab|cafe|cal|call|calvinklein|cam|camera|camp|canon|capetown|capital|capitalone"
137         + "|car|caravan|cards|care|career|careers|cars|casa|case|cash|casino|cat|catering|catholic"
138         + "|cba|cbn|cbre|cbs|center|ceo|cern|cfa|cfd|chanel|channel|charity|chase|chat|cheap|chintai"
139         + "|christmas|chrome|church|cipriani|circle|cisco|citadel|citi|citic|city|cityeats|claims"
140         + "|cleaning|click|clinic|clinique|clothing|cloud|club|clubmed|coach|codes|coffee|college"
141         + "|cologne|com|comcast|commbank|community|company|compare|computer|comsec|condos|construction"
142         + "|consulting|contact|contractors|cooking|cool|coop|corsica|country|coupon|coupons|courses"
143         + "|cpa|credit|creditcard|creditunion|cricket|crown|crs|cruise|cruises|cuisinella|cymru"
144         + "|cyou|c[acdfghiklmnoruvwxyz])"
145         + "|(?:dabur|dad|dance|data|date|dating|datsun|day|dclk|dds|deal|dealer|deals|degree"
146         + "|delivery|dell|deloitte|delta|democrat|dental|dentist|desi|design|dev|dhl|diamonds|diet"
147         + "|digital|direct|directory|discount|discover|dish|diy|dnp|docs|doctor|dog|domains|dot"
148         + "|download|drive|dtv|dubai|dunlop|dupont|durban|dvag|dvr|d[ejkmoz])"
149         + "|(?:earth|eat|eco|edeka|edu|education|email|emerck|energy|engineer|engineering|enterprises"
150         + "|epson|equipment|ericsson|erni|esq|estate|etisalat|eurovision|eus|events|exchange|expert"
151         + "|exposed|express|extraspace|e[cegrstu])"
152         + "|(?:fage|fail|fairwinds|faith|family|fan|fans|farm|farmers|fashion|fast|fedex|feedback"
153         + "|ferrari|ferrero|fidelity|fido|film|final|finance|financial|fire|firestone|firmdale"
154         + "|fish|fishing|fit|fitness|flickr|flights|flir|florist|flowers|fly|foo|food|football"
155         + "|ford|forex|forsale|forum|foundation|fox|free|fresenius|frl|frogans|frontdoor|frontier"
156         + "|ftr|fujitsu|fun|fund|furniture|futbol|fyi|f[ijkmor])"
157         + "|(?:gal|gallery|gallo|gallup|game|games|gap|garden|gay|gbiz|gdn|gea|gent|genting"
158         + "|george|ggee|gift|gifts|gives|giving|glass|gle|global|globo|gmail|gmbh|gmo|gmx|godaddy"
159         + "|gold|goldpoint|golf|goo|goodyear|goog|google|gop|got|gov|grainger|graphics|gratis|green"
160         + "|gripe|grocery|group|guardian|gucci|guge|guide|guitars|guru|g[abdefghilmnpqrstuwy])"
161         + "|(?:hair|hamburg|hangout|haus|hbo|hdfc|hdfcbank|health|healthcare|help|helsinki|here"
162         + "|hermes|hiphop|hisamitsu|hitachi|hiv|hkt|hockey|holdings|holiday|homedepot|homegoods"
163         + "|homes|homesense|honda|horse|hospital|host|hosting|hot|hotels|hotmail|house|how|hsbc"
164         + "|hughes|hyatt|hyundai|h[kmnrtu])"
165         + "|(?:ibm|icbc|ice|icu|ieee|ifm|ikano|imamat|imdb|immo|immobilien|inc|industries|infiniti"
166         + "|info|ing|ink|institute|insurance|insure|int|international|intuit|investments|ipiranga"
167         + "|irish|ismaili|ist|istanbul|itau|itv|i[delmnoqrst])"
168         + "|(?:jaguar|java|jcb|jeep|jetzt|jewelry|jio|jll|jmp|jnj|jobs|joburg|jot|joy|jpmorgan"
169         + "|jprs|juegos|juniper|j[emop])"
170         + "|(?:kaufen|kddi|kerryhotels|kerrylogistics|kerryproperties|kfh|kia|kids|kim|kinder"
171         + "|kindle|kitchen|kiwi|koeln|komatsu|kosher|kpmg|kpn|krd|kred|kuokgroup|kyoto|k[eghimnprwyz])"
172         + "|(?:lacaixa|lamborghini|lamer|lancaster|land|landrover|lanxess|lasalle|lat|latino"
173         + "|latrobe|law|lawyer|lds|lease|leclerc|lefrak|legal|lego|lexus|lgbt|lidl|life|lifeinsurance"
174         + "|lifestyle|lighting|like|lilly|limited|limo|lincoln|link|lipsy|live|living|llc|llp|loan"
175         + "|loans|locker|locus|lol|london|lotte|lotto|love|lpl|lplfinancial|ltd|ltda|lundbeck|luxe"
176         + "|luxury|l[abcikrstuvy])"
177         + "|(?:madrid|maif|maison|makeup|man|management|mango|map|market|marketing|markets|marriott"
178         + "|marshalls|mattel|mba|mckinsey|med|media|meet|melbourne|meme|memorial|men|menu|merckmsd"
179         + "|miami|microsoft|mil|mini|mint|mit|mitsubishi|mlb|mls|mma|mobi|mobile|moda|moe|moi|mom"
180         + "|monash|money|monster|mormon|mortgage|moscow|moto|motorcycles|mov|movie|msd|mtn|mtr"
181         + "|museum|music|m[acdeghklmnopqrstuvwxyz])"
182         + "|(?:nab|nagoya|name|natura|navy|nba|nec|net|netbank|netflix|network|neustar|new|news"
183         + "|next|nextdirect|nexus|nfl|ngo|nhk|nico|nike|nikon|ninja|nissan|nissay|nokia|norton"
184         + "|now|nowruz|nowtv|nra|nrw|ntt|nyc|n[acefgilopruz])"
185         + "|(?:obi|observer|office|okinawa|olayan|olayangroup|oldnavy|ollo|omega|one|ong|onl"
186         + "|online|ooo|open|oracle|orange|org|organic|origins|osaka|otsuka|ott|ovh|om)"
187         + "|(?:page|panasonic|paris|pars|partners|parts|party|pay|pccw|pet|pfizer|pharmacy|phd"
188         + "|philips|phone|photo|photography|photos|physio|pics|pictet|pictures|pid|pin|ping|pink"
189         + "|pioneer|pizza|place|play|playstation|plumbing|plus|pnc|pohl|poker|politie|porn|post"
190         + "|pramerica|praxi|press|prime|pro|prod|productions|prof|progressive|promo|properties"
191         + "|property|protection|pru|prudential|pub|pwc|p[aefghklmnrstwy])"
192         + "|(?:qpon|quebec|quest|qa)"
193         + "|(?:racing|radio|read|realestate|realtor|realty|recipes|red|redstone|redumbrella"
194         + "|rehab|reise|reisen|reit|reliance|ren|rent|rentals|repair|report|republican|rest|restaurant"
195         + "|review|reviews|rexroth|rich|richardli|ricoh|ril|rio|rip|rocher|rocks|rodeo|rogers|room"
196         + "|rsvp|rugby|ruhr|run|rwe|ryukyu|r[eosuw])"
197         + "|(?:saarland|safe|safety|sakura|sale|salon|samsclub|samsung|sandvik|sandvikcoromant"
198         + "|sanofi|sap|sarl|sas|save|saxo|sbi|sbs|sca|scb|schaeffler|schmidt|scholarships|school"
199         + "|schule|schwarz|science|scot|search|seat|secure|security|seek|select|sener|services"
200         + "|seven|sew|sex|sexy|sfr|shangrila|sharp|shaw|shell|shia|shiksha|shoes|shop|shopping"
201         + "|shouji|show|showtime|silk|sina|singles|site|ski|skin|sky|skype|sling|smart|smile|sncf"
202         + "|soccer|social|softbank|software|sohu|solar|solutions|song|sony|soy|spa|space|sport"
203         + "|spot|srl|stada|staples|star|statebank|statefarm|stc|stcgroup|stockholm|storage|store"
204         + "|stream|studio|study|style|sucks|supplies|supply|support|surf|surgery|suzuki|swatch"
205         + "|swiss|sydney|systems|s[abcdeghijklmnorstuvxyz])"
206         + "|(?:tab|taipei|talk|taobao|target|tatamotors|tatar|tattoo|tax|taxi|tci|tdk|team|tech"
207         + "|technology|tel|temasek|tennis|teva|thd|theater|theatre|tiaa|tickets|tienda|tips|tires"
208         + "|tirol|tjmaxx|tjx|tkmaxx|tmall|today|tokyo|tools|top|toray|toshiba|total|tours|town"
209         + "|toyota|toys|trade|trading|training|travel|travelers|travelersinsurance|trust|trv|tube"
210         + "|tui|tunes|tushu|tvs|t[cdfghjklmnortvwz])"
211         + "|(?:ubank|ubs|unicom|university|uno|uol|ups|u[agksyz])"
212         + "|(?:vacations|vana|vanguard|vegas|ventures|verisign|versicherung|vet|viajes|video"
213         + "|vig|viking|villas|vin|vip|virgin|visa|vision|viva|vivo|vlaanderen|vodka|volkswagen"
214         + "|volvo|vote|voting|voto|voyage|v[aceginu])"
215         + "|(?:wales|walmart|walter|wang|wanggou|watch|watches|weather|weatherchannel|webcam"
216         + "|weber|website|wed|wedding|weibo|weir|whoswho|wien|wiki|williamhill|win|windows|wine"
217         + "|winners|wme|wolterskluwer|woodside|work|works|world|wow|wtc|wtf|w[fs])"
218         + "|(?:\u03b5\u03bb|\u03b5\u03c5|\u0431\u0433|\u0431\u0435\u043b|\u0434\u0435\u0442\u0438"
219         + "|\u0435\u044e|\u043a\u0430\u0442\u043e\u043b\u0438\u043a|\u043a\u043e\u043c|\u043c\u043a\u0434"
220         + "|\u043c\u043e\u043d|\u043c\u043e\u0441\u043a\u0432\u0430|\u043e\u043d\u043b\u0430\u0439\u043d"
221         + "|\u043e\u0440\u0433|\u0440\u0443\u0441|\u0440\u0444|\u0441\u0430\u0439\u0442|\u0441\u0440\u0431"
222         + "|\u0443\u043a\u0440|\u049b\u0430\u0437|\u0570\u0561\u0575|\u05d9\u05e9\u05e8\u05d0\u05dc"
223         + "|\u05e7\u05d5\u05dd|\u0627\u0628\u0648\u0638\u0628\u064a|\u0627\u062a\u0635\u0627\u0644\u0627\u062a"
224         + "|\u0627\u0631\u0627\u0645\u0643\u0648|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u0628\u062d\u0631\u064a\u0646"
225         + "|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629"
226         + "|\u0627\u0644\u0639\u0644\u064a\u0627\u0646|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a"
227         + "|\u0627\u06cc\u0631\u0627\u0646|\u0628\u0627\u0631\u062a|\u0628\u0627\u0632\u0627\u0631"
228         + "|\u0628\u064a\u062a\u0643|\u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u062f\u0627\u0646"
229         + "|\u0633\u0648\u0631\u064a\u0629|\u0634\u0628\u0643\u0629|\u0639\u0631\u0627\u0642|\u0639\u0631\u0628"
230         + "|\u0639\u0645\u0627\u0646|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0643\u0627\u062b\u0648\u0644\u064a\u0643"
231         + "|\u0643\u0648\u0645|\u0645\u0635\u0631|\u0645\u0644\u064a\u0633\u064a\u0627|\u0645\u0648\u0631\u064a\u062a\u0627\u0646\u064a\u0627"
232         + "|\u0645\u0648\u0642\u0639|\u0647\u0645\u0631\u0627\u0647|\u067e\u0627\u06a9\u0633\u062a\u0627\u0646"
233         + "|\u0680\u0627\u0631\u062a|\u0915\u0949\u092e|\u0928\u0947\u091f|\u092d\u093e\u0930\u0924"
234         + "|\u092d\u093e\u0930\u0924\u092e\u094d|\u092d\u093e\u0930\u094b\u0924|\u0938\u0902\u0917\u0920\u0928"
235         + "|\u09ac\u09be\u0982\u09b2\u09be|\u09ad\u09be\u09b0\u09a4|\u09ad\u09be\u09f0\u09a4|\u0a2d\u0a3e\u0a30\u0a24"
236         + "|\u0aad\u0abe\u0ab0\u0aa4|\u0b2d\u0b3e\u0b30\u0b24|\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe"
237         + "|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd"
238         + "|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0cad\u0cbe\u0cb0\u0ca4|\u0d2d\u0d3e\u0d30\u0d24\u0d02"
239         + "|\u0dbd\u0d82\u0d9a\u0dcf|\u0e04\u0e2d\u0e21|\u0e44\u0e17\u0e22|\u0ea5\u0eb2\u0ea7|\u10d2\u10d4"
240         + "|\u307f\u3093\u306a|\u30a2\u30de\u30be\u30f3|\u30af\u30e9\u30a6\u30c9|\u30b0\u30fc\u30b0\u30eb"
241         + "|\u30b3\u30e0|\u30b9\u30c8\u30a2|\u30bb\u30fc\u30eb|\u30d5\u30a1\u30c3\u30b7\u30e7\u30f3"
242         + "|\u30dd\u30a4\u30f3\u30c8|\u4e16\u754c|\u4e2d\u4fe1|\u4e2d\u56fd|\u4e2d\u570b|\u4e2d\u6587\u7f51"
243         + "|\u4e9a\u9a6c\u900a|\u4f01\u4e1a|\u4f5b\u5c71|\u4fe1\u606f|\u5065\u5eb7|\u516b\u5366"
244         + "|\u516c\u53f8|\u516c\u76ca|\u53f0\u6e7e|\u53f0\u7063|\u5546\u57ce|\u5546\u5e97|\u5546\u6807"
245         + "|\u5609\u91cc|\u5609\u91cc\u5927\u9152\u5e97|\u5728\u7ebf|\u5927\u62ff|\u5929\u4e3b\u6559"
246         + "|\u5a31\u4e50|\u5bb6\u96fb|\u5e7f\u4e1c|\u5fae\u535a|\u6148\u5584|\u6211\u7231\u4f60"
247         + "|\u624b\u673a|\u62db\u8058|\u653f\u52a1|\u653f\u5e9c|\u65b0\u52a0\u5761|\u65b0\u95fb"
248         + "|\u65f6\u5c1a|\u66f8\u7c4d|\u673a\u6784|\u6de1\u9a6c\u9521|\u6e38\u620f|\u6fb3\u9580"
249         + "|\u70b9\u770b|\u79fb\u52a8|\u7ec4\u7ec7\u673a\u6784|\u7f51\u5740|\u7f51\u5e97|\u7f51\u7ad9"
250         + "|\u7f51\u7edc|\u8054\u901a|\u8c37\u6b4c|\u8d2d\u7269|\u901a\u8ca9|\u96c6\u56e2|\u96fb\u8a0a\u76c8\u79d1"
251         + "|\u98de\u5229\u6d66|\u98df\u54c1|\u9910\u5385|\u9999\u683c\u91cc\u62c9|\u9999\u6e2f"
252         + "|\ub2f7\ub137|\ub2f7\ucef4|\uc0bc\uc131|\ud55c\uad6d"
253         + "|xbox|xerox|xfinity|xihuan|xin|xn\\-\\-11b4c3d|xn\\-\\-1ck2e1b|xn\\-\\-1qqw23a|xn\\-\\-2scrj9c"
254         + "|xn\\-\\-30rr7y|xn\\-\\-3bst00m|xn\\-\\-3ds443g|xn\\-\\-3e0b707e|xn\\-\\-3hcrj9c|xn\\-\\-3pxu8k"
255         + "|xn\\-\\-42c2d9a|xn\\-\\-45br5cyl|xn\\-\\-45brj9c|xn\\-\\-45q11c|xn\\-\\-4dbrk0ce|xn\\-\\-4gbrim"
256         + "|xn\\-\\-54b7fta0cc|xn\\-\\-55qw42g|xn\\-\\-55qx5d|xn\\-\\-5su34j936bgsg|xn\\-\\-5tzm5g"
257         + "|xn\\-\\-6frz82g|xn\\-\\-6qq986b3xl|xn\\-\\-80adxhks|xn\\-\\-80ao21a|xn\\-\\-80aqecdr1a"
258         + "|xn\\-\\-80asehdb|xn\\-\\-80aswg|xn\\-\\-8y0a063a|xn\\-\\-90a3ac|xn\\-\\-90ae|xn\\-\\-90ais"
259         + "|xn\\-\\-9dbq2a|xn\\-\\-9et52u|xn\\-\\-9krt00a|xn\\-\\-b4w605ferd|xn\\-\\-bck1b9a5dre4c"
260         + "|xn\\-\\-c1avg|xn\\-\\-c2br7g|xn\\-\\-cck2b3b|xn\\-\\-cckwcxetd|xn\\-\\-cg4bki|xn\\-\\-clchc0ea0b2g2a9gcd"
261         + "|xn\\-\\-czr694b|xn\\-\\-czrs0t|xn\\-\\-czru2d|xn\\-\\-d1acj3b|xn\\-\\-d1alf|xn\\-\\-e1a4c"
262         + "|xn\\-\\-eckvdtc9d|xn\\-\\-efvy88h|xn\\-\\-fct429k|xn\\-\\-fhbei|xn\\-\\-fiq228c5hs"
263         + "|xn\\-\\-fiq64b|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fjq720a|xn\\-\\-flw351e|xn\\-\\-fpcrj9c3d"
264         + "|xn\\-\\-fzc2c9e2c|xn\\-\\-fzys8d69uvgm|xn\\-\\-g2xx48c|xn\\-\\-gckr3f0f|xn\\-\\-gecrj9c"
265         + "|xn\\-\\-gk3at1e|xn\\-\\-h2breg3eve|xn\\-\\-h2brj9c|xn\\-\\-h2brj9c8c|xn\\-\\-hxt814e"
266         + "|xn\\-\\-i1b6b1a6a2e|xn\\-\\-imr513n|xn\\-\\-io0a7i|xn\\-\\-j1aef|xn\\-\\-j1amh|xn\\-\\-j6w193g"
267         + "|xn\\-\\-jlq480n2rg|xn\\-\\-jvr189m|xn\\-\\-kcrx77d1x4a|xn\\-\\-kprw13d|xn\\-\\-kpry57d"
268         + "|xn\\-\\-kput3i|xn\\-\\-l1acc|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgb9awbf|xn\\-\\-mgba3a3ejt"
269         + "|xn\\-\\-mgba3a4f16a|xn\\-\\-mgba7c0bbn0a|xn\\-\\-mgbaakc7dvf|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbab2bd"
270         + "|xn\\-\\-mgbah1a3hjkrd|xn\\-\\-mgbai9azgqp6j|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbbh1a|xn\\-\\-mgbbh1a71e"
271         + "|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgbca7dzdo|xn\\-\\-mgbcpq6gpa1a|xn\\-\\-mgberp4a5d4ar|xn\\-\\-mgbgu82a"
272         + "|xn\\-\\-mgbi4ecexp|xn\\-\\-mgbpl2fh|xn\\-\\-mgbt3dhd|xn\\-\\-mgbtx2b|xn\\-\\-mgbx4cd0ab"
273         + "|xn\\-\\-mix891f|xn\\-\\-mk1bu44c|xn\\-\\-mxtq1m|xn\\-\\-ngbc5azd|xn\\-\\-ngbe9e0a|xn\\-\\-ngbrx"
274         + "|xn\\-\\-node|xn\\-\\-nqv7f|xn\\-\\-nqv7fs00ema|xn\\-\\-nyqy26a|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl"
275         + "|xn\\-\\-otu796d|xn\\-\\-p1acf|xn\\-\\-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-pssy2u|xn\\-\\-q7ce6a"
276         + "|xn\\-\\-q9jyb4c|xn\\-\\-qcka1pmc|xn\\-\\-qxa6a|xn\\-\\-qxam|xn\\-\\-rhqv96g|xn\\-\\-rovu88b"
277         + "|xn\\-\\-rvc1e0am3e|xn\\-\\-s9brj9c|xn\\-\\-ses554g|xn\\-\\-t60b56a|xn\\-\\-tckwe|xn\\-\\-tiq49xqyj"
278         + "|xn\\-\\-unup4y|xn\\-\\-vermgensberater\\-ctb|xn\\-\\-vermgensberatung\\-pwb|xn\\-\\-vhquv"
279         + "|xn\\-\\-vuq861b|xn\\-\\-w4r85el8fhu5dnra|xn\\-\\-w4rs40l|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a"
280         + "|xn\\-\\-xhq521b|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-y9a3aq|xn\\-\\-yfro4i67o"
281         + "|xn\\-\\-ygbi2ammx|xn\\-\\-zfr164b|xxx|xyz)"
282         + "|(?:yachts|yahoo|yamaxun|yandex|yodobashi|yoga|yokohama|you|youtube|yun|y[et])"
283         + "|(?:zappos|zara|zero|zip|zone|zuerich|z[amw]))";
284     /**
285      * Kept for backward compatibility reasons.
286      *
287      * @deprecated Deprecated since it does not include all IRI characters defined in RFC 3987
288      */
289     @Deprecated
290     public static final String GOOD_IRI_CHAR =
291         "a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
292 
293     private static final String IP_ADDRESS_STRING =
294             "((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]"
295             + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
296             + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
297             + "|[1-9][0-9]|[0-9]))";
298 
299     /**
300      * Kept for backward compatibility reasons. It does not match IPv6 addresses.
301      *
302      * @deprecated Please use {@link android.net.InetAddresses#isNumericAddress(String)} instead.
303      */
304     @Deprecated
305     public static final Pattern IP_ADDRESS = Pattern.compile(IP_ADDRESS_STRING);
306 
307     /**
308      * Valid UCS characters defined in RFC 3987. Excludes space characters.
309      */
310     private static final String UCS_CHAR = "[" +
311             "\u00A0-\uD7FF" +
312             "\uF900-\uFDCF" +
313             "\uFDF0-\uFFEF" +
314             "\uD800\uDC00-\uD83F\uDFFD" +
315             "\uD840\uDC00-\uD87F\uDFFD" +
316             "\uD880\uDC00-\uD8BF\uDFFD" +
317             "\uD8C0\uDC00-\uD8FF\uDFFD" +
318             "\uD900\uDC00-\uD93F\uDFFD" +
319             "\uD940\uDC00-\uD97F\uDFFD" +
320             "\uD980\uDC00-\uD9BF\uDFFD" +
321             "\uD9C0\uDC00-\uD9FF\uDFFD" +
322             "\uDA00\uDC00-\uDA3F\uDFFD" +
323             "\uDA40\uDC00-\uDA7F\uDFFD" +
324             "\uDA80\uDC00-\uDABF\uDFFD" +
325             "\uDAC0\uDC00-\uDAFF\uDFFD" +
326             "\uDB00\uDC00-\uDB3F\uDFFD" +
327             "\uDB44\uDC00-\uDB7F\uDFFD" +
328             "&&[^\u00A0[\u2000-\u200A]\u2028\u2029\u202F\u3000]]";
329 
330     /**
331      * Valid characters for IRI label defined in RFC 3987.
332      */
333     private static final String LABEL_CHAR = "a-zA-Z0-9" + UCS_CHAR;
334 
335     /**
336      * Valid characters for IRI TLD defined in RFC 3987.
337      */
338     private static final String TLD_CHAR = "a-zA-Z" + UCS_CHAR;
339 
340     /**
341      * RFC 1035 Section 2.3.4 limits the labels to a maximum 63 octets.
342      */
343     private static final String IRI_LABEL =
344             "[" + LABEL_CHAR + "](?:[" + LABEL_CHAR + "_\\-]{0,61}[" + LABEL_CHAR + "]){0,1}";
345 
346     /**
347      * RFC 3492 references RFC 1034 and limits Punycode algorithm output to 63 characters.
348      */
349     private static final String PUNYCODE_TLD = "xn\\-\\-[\\w\\-]{0,58}\\w";
350 
351     private static final String TLD = "(" + PUNYCODE_TLD + "|" + "[" + TLD_CHAR + "]{2,63}" +")";
352 
353     private static final String HOST_NAME = "(" + IRI_LABEL + "\\.)+" + TLD;
354 
355     private static final String DOMAIN_NAME_STR = "(" + HOST_NAME + "|" + IP_ADDRESS_STRING + ")";
356     public static final Pattern DOMAIN_NAME = Pattern.compile(DOMAIN_NAME_STR);
357 
358     private static final String PROTOCOL = "(?i:http|https|rtsp|ftp)://";
359 
360     /* A word boundary or end of input.  This is to stop foo.sure from matching as foo.su */
361     private static final String WORD_BOUNDARY = "(?:\\b|$|^)";
362 
363     private static final String USER_INFO = "(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
364             + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
365             + "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@";
366 
367     private static final String PORT_NUMBER = "\\:\\d{1,5}";
368 
369     private static final String PATH_AND_QUERY = "[/\\?](?:(?:[" + LABEL_CHAR
370             + ";/\\?:@&=#~"  // plus optional query params
371             + "\\-\\.\\+!\\*'\\(\\),_\\$])|(?:%[a-fA-F0-9]{2}))*";
372 
373     /**
374      *  Regular expression pattern to match most part of RFC 3987
375      *  Internationalized URLs, aka IRIs.
376      */
377     public static final Pattern WEB_URL = Pattern.compile("("
378             + "("
379             + "(?:" + PROTOCOL + "(?:" + USER_INFO + ")?" + ")?"
380             + "(?:" + DOMAIN_NAME_STR + ")"
381             + "(?:" + PORT_NUMBER + ")?"
382             + ")"
383             + "(" + PATH_AND_QUERY + ")?"
384             + WORD_BOUNDARY
385             + ")");
386 
387     /**
388      * Regular expression that matches known TLDs and punycode TLDs
389      */
390     private static final String STRICT_TLD = "(?:" +
391             IANA_TOP_LEVEL_DOMAINS + "|" + PUNYCODE_TLD + ")";
392 
393     /**
394      * Regular expression that matches host names using {@link #STRICT_TLD}
395      */
396     private static final String STRICT_HOST_NAME = "(?:(?:" + IRI_LABEL + "\\.)+"
397             + STRICT_TLD + ")";
398 
399     /**
400      * Regular expression that matches domain names using either {@link #STRICT_HOST_NAME} or
401      * {@link #IP_ADDRESS}
402      */
403     private static final String STRICT_DOMAIN_NAME = "(?:" + STRICT_HOST_NAME + "|"
404             + IP_ADDRESS_STRING + ")";
405 
406     /**
407      * Regular expression that matches domain names without a TLD
408      */
409     private static final String RELAXED_DOMAIN_NAME =
410             "(?:" + "(?:" + IRI_LABEL + "(?:\\.(?=\\S))" +"?)+" + "|" + IP_ADDRESS_STRING + ")";
411 
412     /**
413      * Regular expression to match strings that do not start with a supported protocol. The TLDs
414      * are expected to be one of the known TLDs.
415      */
416     private static final String WEB_URL_WITHOUT_PROTOCOL = "("
417             + WORD_BOUNDARY
418             + "(?<!:\\/\\/)"
419             + "("
420             + "(?:" + STRICT_DOMAIN_NAME + ")"
421             + "(?:" + PORT_NUMBER + ")?"
422             + ")"
423             + "(?:" + PATH_AND_QUERY + ")?"
424             + WORD_BOUNDARY
425             + ")";
426 
427     /**
428      * Regular expression to match strings that start with a supported protocol. Rules for domain
429      * names and TLDs are more relaxed. TLDs are optional.
430      */
431     private static final String WEB_URL_WITH_PROTOCOL = "("
432             + WORD_BOUNDARY
433             + "(?:"
434             + "(?:" + PROTOCOL + "(?:" + USER_INFO + ")?" + ")"
435             + "(?:" + RELAXED_DOMAIN_NAME + ")?"
436             + "(?:" + PORT_NUMBER + ")?"
437             + ")"
438             + "(?:" + PATH_AND_QUERY + ")?"
439             + WORD_BOUNDARY
440             + ")";
441 
442     /**
443      * Regular expression pattern to match IRIs. If a string starts with http(s):// the expression
444      * tries to match the URL structure with a relaxed rule for TLDs. If the string does not start
445      * with http(s):// the TLDs are expected to be one of the known TLDs.
446      *
447      * @hide
448      */
449     public static final Pattern AUTOLINK_WEB_URL = Pattern.compile(
450             "(" + WEB_URL_WITH_PROTOCOL + "|" + WEB_URL_WITHOUT_PROTOCOL + ")");
451 
452     /**
453      * Regular expression for valid email characters. Does not include some of the valid characters
454      * defined in RFC5321: #&~!^`{}/=$*?|
455      */
456     private static final String EMAIL_CHAR = LABEL_CHAR + "\\+\\-_%'";
457 
458     /**
459      * Regular expression for local part of an email address. RFC5321 section 4.5.3.1.1 limits
460      * the local part to be at most 64 octets.
461      */
462     private static final String EMAIL_ADDRESS_LOCAL_PART =
463             "[" + EMAIL_CHAR + "]" + "(?:[" + EMAIL_CHAR + "\\.]{0,62}[" + EMAIL_CHAR + "])?";
464 
465     /**
466      * Regular expression for the domain part of an email address. RFC5321 section 4.5.3.1.2 limits
467      * the domain to be at most 255 octets.
468      */
469     private static final String EMAIL_ADDRESS_DOMAIN =
470             "(?=.{1,255}(?:\\s|$|^))" + HOST_NAME;
471 
472     /**
473      * Regular expression pattern to match email addresses. It excludes double quoted local parts
474      * and the special characters #&~!^`{}/=$*?| that are included in RFC5321.
475      * @hide
476      */
477     public static final Pattern AUTOLINK_EMAIL_ADDRESS = Pattern.compile("(" + WORD_BOUNDARY +
478             "(?:" + EMAIL_ADDRESS_LOCAL_PART + "@" + EMAIL_ADDRESS_DOMAIN + ")" +
479             WORD_BOUNDARY + ")"
480     );
481 
482     public static final Pattern EMAIL_ADDRESS
483         = Pattern.compile(
484             "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
485             "\\@" +
486             "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
487             "(" +
488                 "\\." +
489                 "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
490             ")+"
491         );
492 
493     /**
494      * This pattern is intended for searching for things that look like they
495      * might be phone numbers in arbitrary text, not for validating whether
496      * something is in fact a phone number.  It will miss many things that
497      * are legitimate phone numbers.
498      *
499      * <p> The pattern matches the following:
500      * <ul>
501      * <li>Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes
502      * may follow.
503      * <li>Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
504      * <li>A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
505      * </ul>
506      */
507     public static final Pattern PHONE
508         = Pattern.compile(                      // sdd = space, dot, or dash
509                 "(\\+[0-9]+[\\- \\.]*)?"        // +<digits><sdd>*
510                 + "(\\([0-9]+\\)[\\- \\.]*)?"   // (<digits>)<sdd>*
511                 + "([0-9][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit>
512 
513     /**
514      *  Convenience method to take all of the non-null matching groups in a
515      *  regex Matcher and return them as a concatenated string.
516      *
517      *  @param matcher      The Matcher object from which grouped text will
518      *                      be extracted
519      *
520      *  @return             A String comprising all of the non-null matched
521      *                      groups concatenated together
522      */
concatGroups(Matcher matcher)523     public static final String concatGroups(Matcher matcher) {
524         StringBuilder b = new StringBuilder();
525         final int numGroups = matcher.groupCount();
526 
527         for (int i = 1; i <= numGroups; i++) {
528             String s = matcher.group(i);
529 
530             if (s != null) {
531                 b.append(s);
532             }
533         }
534 
535         return b.toString();
536     }
537 
538     /**
539      * Convenience method to return only the digits and plus signs
540      * in the matching string.
541      *
542      * @param matcher      The Matcher object from which digits and plus will
543      *                     be extracted
544      *
545      * @return             A String comprising all of the digits and plus in
546      *                     the match
547      */
digitsAndPlusOnly(Matcher matcher)548     public static final String digitsAndPlusOnly(Matcher matcher) {
549         StringBuilder buffer = new StringBuilder();
550         String matchingRegion = matcher.group();
551 
552         for (int i = 0, size = matchingRegion.length(); i < size; i++) {
553             char character = matchingRegion.charAt(i);
554 
555             if (character == '+' || Character.isDigit(character)) {
556                 buffer.append(character);
557             }
558         }
559         return buffer.toString();
560     }
561 
562     /**
563      * Do not create this static utility class.
564      */
Patterns()565     private Patterns() {}
566 }
567