FileDocCategorySizeDatePackage
Patterns.javaAPI DocAndroid 5.1 API12839Thu Mar 12 22:22:10 GMT 2015android.util

Patterns

public class Patterns extends Object
Commonly used regular expression patterns.

Fields Summary
public static final String
TOP_LEVEL_DOMAIN_STR
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 frameworks/ex/common/tools/make-iana-tld-pattern.py
public static final Pattern
TOP_LEVEL_DOMAIN
Regular expression pattern to match all IANA top-level domains.
public static final String
TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL
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 frameworks/ex/common/tools/make-iana-tld-pattern.py
public static final String
GOOD_IRI_CHAR
Good characters for Internationalized Resource Identifiers (IRI). This comprises most common used Unicode characters allowed in IRI as detailed in RFC 3987. Specifically, those two byte Unicode characters are not included.
public static final Pattern
IP_ADDRESS
private static final String
IRI
RFC 1035 Section 2.3.4 limits the labels to a maximum 63 octets.
private static final String
GOOD_GTLD_CHAR
private static final String
GTLD
private static final String
HOST_NAME
public static final Pattern
DOMAIN_NAME
public static final Pattern
WEB_URL
Regular expression pattern to match most part of RFC 3987 Internationalized URLs, aka IRIs. Commonly used Unicode characters are added.
public static final Pattern
EMAIL_ADDRESS
public static final Pattern
PHONE
This pattern is intended for searching for things that look like they might be phone numbers in arbitrary text, not for validating whether something is in fact a phone number. It will miss many things that are legitimate phone numbers.

The pattern matches the following:

  • Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes may follow.
  • Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
  • A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
Constructors Summary
private Patterns()
Do not create this static utility class.

Methods Summary
public static final java.lang.StringconcatGroups(java.util.regex.Matcher matcher)
Convenience method to take all of the non-null matching groups in a regex Matcher and return them as a concatenated string.

param
matcher The Matcher object from which grouped text will be extracted
return
A String comprising all of the non-null matched groups concatenated together

 // <digit><digit|sdd>+<digit>

                                                                                                                     
          
        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();
    
public static final java.lang.StringdigitsAndPlusOnly(java.util.regex.Matcher matcher)
Convenience method to return only the digits and plus signs in the matching string.

param
matcher The Matcher object from which digits and plus will be extracted
return
A String comprising all of the digits and plus in the match

        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();