MatchAll() public method

public MatchAll ( String value ) : Match
value String
return System.Text.RegularExpressions.Match
Example #1
0
        private bool MatchesEmergencyNumberHelper(String number, String regionCode,
                                                  bool allowPrefixMatch)
        {
            number = PhoneNumberUtil.ExtractPossibleNumber(number);
            if (PhoneNumberUtil.PlusCharsPattern.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);
            }
            var metadata = phoneUtil.GetMetadataForRegion(regionCode);

            if (metadata == null || !metadata.HasEmergency)
            {
                return(false);
            }
            var emergencyNumberPattern =
                new PhoneRegex(metadata.Emergency.NationalNumberPattern);
            var normalizedNumber = PhoneNumberUtil.NormalizeDigitsOnly(number);

            // In Brazil, it is impossible to append additional digits to an emergency number to dial the
            // number.
            return((!allowPrefixMatch || regionCode.Equals("BR"))
                ? emergencyNumberPattern.MatchAll(normalizedNumber).Success
                : emergencyNumberPattern.MatchBeginning(normalizedNumber).Success);
        }
        /**
         * Parses a phone number from the {@code candidate} using {@link PhoneNumberUtil#parse} and
         * verifies it matches the requested {@link #leniency}. If parsing and verification succeed, a
         * corresponding {@link PhoneNumberMatch} is returned, otherwise this method returns null.
         *
         * @param candidate  the candidate match
         * @param offset  the offset of {@code candidate} within {@link #text}
         * @return  the parsed and validated phone number match, or null
         */
        private PhoneNumberMatch ParseAndVerify(String candidate, int offset)
        {
            // Check the candidate doesn't contain any formatting which would indicate that it really
            // isn't a phone number.
            if (!MATCHING_BRACKETS.MatchAll(candidate).Success)
            {
                return(null);
            }

            // If leniency is set to VALID or stricter, we also want to skip numbers that are surrounded
            // by Latin alphabetic characters, to skip cases like abc8005001234 or 8005001234def.
            if (leniency >= PhoneNumberUtil.Leniency.VALID)
            {
                // If the candidate is not at the start of the text, and does not start with phone-number
                // punctuation, check the previous character.
                if (offset > 0 && !LEAD_CLASS.MatchBeginning(candidate).Success)
                {
                    char previousChar = text[offset - 1];
                    // We return null if it is a latin letter or an invalid punctuation symbol.
                    if (IsInvalidPunctuationSymbol(previousChar) || IsLatinLetter(previousChar))
                    {
                        return(null);
                    }
                }
                int lastCharIndex = offset + candidate.Length;
                if (lastCharIndex < text.Length)
                {
                    char nextChar = text[lastCharIndex];
                    if (IsInvalidPunctuationSymbol(nextChar) || IsLatinLetter(nextChar))
                    {
                        return(null);
                    }
                }
            }

            PhoneNumber number;
            var         result = phoneUtil.ParseAndKeepRawInput(candidate, preferredRegion, out number);

            if (result != ErrorType.NO_ERROR)
            {
                return(null);
            }

            if (phoneUtil.Verify(leniency, number, candidate, phoneUtil))
            {
                // We used parseAndKeepRawInput to create this number, but for now we don't return the extra
                // values parsed. TODO: stop clearing all values here and switch all users over
                // to using rawInput() rather than the rawString() of PhoneNumberMatch.
                var bnumber = number.ToBuilder();
                bnumber.ClearCountryCodeSource();
                bnumber.ClearRawInput();
                bnumber.ClearPreferredDomesticCarrierCode();
                return(new PhoneNumberMatch(offset, candidate, bnumber.Build()));
            }

            return(null);
        }
        private void GetAvailableFormats(string leadingDigits)
        {
            // First decide whether we should use international or national number rules.
            var isInternationalNumber = isCompleteNumber && extractedNationalPrefix.Length == 0;
            var formatList            =
                isInternationalNumber && currentMetadata.IntlNumberFormatCount > 0
                    ? currentMetadata.IntlNumberFormatList
                    : currentMetadata.NumberFormatList;

            foreach (var format in formatList)
            {
                // Discard a few formats that we know are not relevant based on the presence of the national
                // prefix.
                if (extractedNationalPrefix.Length > 0 &&
                    PhoneNumberUtil.FormattingRuleHasFirstGroupOnly(
                        format.NationalPrefixFormattingRule) &&
                    !format.NationalPrefixOptionalWhenFormatting &&
                    !format.HasDomesticCarrierCodeFormattingRule)
                {
                    // If it is a national number that had a national prefix, any rules that aren't valid with a
                    // national prefix should be excluded. A rule that has a carrier-code formatting rule is
                    // kept since the national prefix might actually be an extracted carrier code - we don't
                    // distinguish between these when extracting it in the AYTF.
                    continue;
                }
                else if (extractedNationalPrefix.Length == 0 &&
                         !isCompleteNumber &&
                         !PhoneNumberUtil.FormattingRuleHasFirstGroupOnly(
                             format.NationalPrefixFormattingRule) &&
                         !format.NationalPrefixOptionalWhenFormatting)
                {
                    // This number was entered without a national prefix, and this formatting rule requires one,
                    // so we discard it.
                    continue;
                }
                if (EligibleFormatPattern.MatchAll(format.Format).Success)
                {
                    possibleFormats.Add(format);
                }
            }
            NarrowDownPossibleFormats(leadingDigits);
        }
        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 = phoneUtil.GetMetadataForRegion(regionCode);
            if (metadata == null || !metadata.HasEmergency)
            {
                return false;
            }
            var emergencyNumberPattern =
                new PhoneRegex(metadata.Emergency.NationalNumberPattern);
            String normalizedNumber = PhoneNumberUtil.NormalizeDigitsOnly(number);
            // In Brazil, it is impossible to append additional digits to an emergency number to dial the
            // number.
            return (!allowPrefixMatch || regionCode.Equals("BR"))
                ? emergencyNumberPattern.MatchAll(normalizedNumber).Success
                : emergencyNumberPattern.MatchBeginning(normalizedNumber).Success;

        }
 /**
 * Helper method to check a number against a particular pattern and determine whether it matches,
 * or is too short or too long. Currently, if a number pattern suggests that numbers of length 7
 * and 10 are possible, and a number in between these possible lengths is entered, such as of
 * length 8, this will return TOO_LONG.
 */
 private ValidationResult TestNumberLengthAgainstPattern(PhoneRegex numberPattern, String number)
 {
     if (numberPattern.MatchAll(number).Success)
         return ValidationResult.IS_POSSIBLE;
     if (numberPattern.MatchBeginning(number).Success)
         return ValidationResult.TOO_LONG;
     return ValidationResult.TOO_SHORT;
 }
        /**
        * Returns true if the number matches the short code number format for the given region.
        *
        * This method takes into account cases where the number might contain formatting, but doesn't
        * allow additional digits to be appended.
        *
        * @param number  the phone number to test
        * @param regionCode  the region where the phone number is being dialed
        * @return  if the number matches the short code number format for the given region.
        */
        public bool IsShortcodeNumber(string number, string regionCode)
        {
            var phoneMetadataForRegion = phoneUtil.GetMetadataForRegion(regionCode);
            if (phoneMetadataForRegion == null || !phoneMetadataForRegion.HasShortCode) {
                // NOTE: We should also probably do this when phoneMetadataForRegion.ShortCode.NationalNumberPattern.Equals("NA")
                // I think there is a bug where PhoneNumbers.BuildMetadataFromXml.LoadGeneralDesc always calls metadata.SetShortCode(),
                // which always sets HasShortCode to true, even in the cases where PhoneNumbers.BuildMetadataFromXml.ProcessPhoneNumberDescElement
                // returns "NA" (which happens when the territory in PhoneNumberMetaData.xml does not contain a shortCode definition/node)
                return false;
            }

            var shortCodeNumberPattern = new PhoneRegex(phoneMetadataForRegion.ShortCode.NationalNumberPattern);
            var normalizedNumber = PhoneNumberUtil.NormalizeDigitsOnly(number);

            return shortCodeNumberPattern.MatchAll(normalizedNumber).Success;
        }
 private bool IsFormatEligible(String format)
 {
     return(ELIGIBLE_FORMAT_PATTERN.MatchAll(format).Success);
 }
Example #8
0
 private static bool IsFormatEligible(string format)
 {
     return(EligibleFormatPattern.MatchAll(format).Success);
 }