コード例 #1
0
        private bool matchesEmergencyNumberHelper(String number, String regionCode,
                                                  bool allowPrefixMatch)
        {
            number = PhoneNumberUtil.extractPossibleNumber(number);
            if (PhoneNumberUtil.PLUS_CHARS_PATTERN.MatchBeginning(number).Success)
            {
                // Returns false if the number starts with a plus sign. We don't believe dialing the country
                // code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can
                // add additional logic here to handle it.
                return(false);
            }
            PhoneMetadata metadata = MetadataManager.getShortNumberMetadataForRegion(regionCode);

            if (metadata == null || !metadata.HasEmergency())
            {
                return(false);
            }
            var emergencyNumberPattern =
                new JavaRegex(metadata.getEmergency().getNationalNumberPattern());
            String normalizedNumber = PhoneNumberUtil.normalizeDigitsOnly(number);

            return((!allowPrefixMatch || REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT.Contains(regionCode))
        ? emergencyNumberPattern.MatchWhole(normalizedNumber).Success
        : emergencyNumberPattern.MatchBeginning(normalizedNumber).Success);
        }
コード例 #2
0
        /**
         * Trims away any characters after the first match of {@code Regex} in {@code candidate},
         * returning the trimmed version.
         */
        private static string trimAfterFirstMatch(JavaRegex regex, string candidate)
        {
            var trailingCharsMatcher = regex.Match(candidate);

            if (trailingCharsMatcher.Success)
            {
                candidate = candidate.Substring(0, trailingCharsMatcher.Index);
            }
            return(candidate);
        }
コード例 #3
0
        static PhoneNumberMatcher()
        {
            /* Builds the MATCHING_BRACKETS and Regex regular expressions. The building blocks below exist
             * to make the Regex more easily understood. */

            String openingParens = "(\\[\uFF08\uFF3B";
            String closingParens = ")\\]\uFF09\uFF3D";
            String nonParens     = "[^" + openingParens + closingParens + "]";

            /* Limit on the number of pairs of brackets in a phone number. */
            String bracketPairLimit = limit(0, 3);

            /*
             * An opening bracket at the beginning may not be closed, but subsequent ones should be.  It's
             * also possible that the leading bracket was dropped, so we shouldn't be surprised if we see a
             * closing bracket first. We limit the sets of brackets in a phone number to four.
             */
            MATCHING_BRACKETS = new JavaRegex(
                "(?:[" + openingParens + "])?" + "(?:" + nonParens + "+" + "[" + closingParens + "])?" +
                nonParens + "+" +
                "(?:[" + openingParens + "]" + nonParens + "+[" + closingParens + "])" + bracketPairLimit +
                nonParens + "*");

            /* Limit on the number of leading (plus) characters. */
            String leadLimit = limit(0, 2);
            /* Limit on the number of consecutive punctuation characters. */
            String punctuationLimit = limit(0, 4);

            /* The maximum number of digits allowed in a digit-separated block. As we allow all digits in a
             * single block, set high enough to accommodate the entire national number and the international
             * country code. */
            int digitBlockLimit =
                PhoneNumberUtil.MAX_LENGTH_FOR_NSN + PhoneNumberUtil.MAX_LENGTH_COUNTRY_CODE;

            /* Limit on the number of blocks separated by punctuation. Uses digitBlockLimit since some
             * formats use spaces to separate each digit. */
            String blockLimit = limit(0, digitBlockLimit);

            /* A punctuation sequence allowing white space. */
            String punctuation = "[" + PhoneNumberUtil.VALID_PUNCTUATION + "]" + punctuationLimit;
            /* A digits block without punctuation. */
            String digitSequence = "\\p{Nd}" + limit(1, digitBlockLimit);

            String leadClassChars = openingParens + PhoneNumberUtil.PLUS_CHARS;
            String leadClass      = "[" + leadClassChars + "]";

            LEAD_CLASS = new JavaRegex(leadClass);

            /* Phone number Regex allowing optional punctuation. */
            PATTERN = new JavaRegex(
                "(?:" + leadClass + punctuation + ")" + leadLimit +
                digitSequence + "(?:" + punctuation + digitSequence + ")" + blockLimit +
                "(?:" + PhoneNumberUtil.EXTN_PATTERNS_FOR_MATCHING + ")?",
                PhoneNumberUtil.REGEX_FLAGS);
        }
コード例 #4
0
        public JavaRegex getRegexForRegex(String regex)
        {
            var Regex = _cache.Get(regex);

            if (Regex == null)
            {
                Regex = new JavaRegex(regex);
                _cache.Add(regex, Regex);
            }
            return(Regex);
        }
コード例 #5
0
        private void narrowDownPossibleFormats(String leadingDigits)
        {
            int indexOfLeadingDigitsRegex = leadingDigits.Length - MIN_LEADING_DIGITS_LENGTH;

            foreach (var format in possibleFormats.ToArray())
            {
                if (format.leadingDigitsPatternSize() == 0)
                {
                    // Keep everything that isn't restricted by leading digits.
                    continue;
                }
                int lastLeadingDigitsRegex =
                    Math.Min(indexOfLeadingDigitsRegex, format.leadingDigitsPatternSize() - 1);
                JavaRegex leadingDigitsRegex = regexCache.getRegexForRegex(
                    format.getLeadingDigitsPattern(lastLeadingDigitsRegex));
                var m = leadingDigitsRegex.MatchBeginning(leadingDigits);
                if (!m.Success)
                {
                    possibleFormats.Remove(format);
                }
            }
        }