// Returns true if a new template is created as opposed to reusing the existing template.
 private bool MaybeCreateNewTemplate()
 {
     // When there are multiple available formats, the formatter uses the first format where a
     // formatting template could be created.
     while (possibleFormats.Count > 0)
     {
         var numberFormat = possibleFormats[0];
         var pattern      = numberFormat.Pattern;
         if (currentFormattingPattern.Equals(pattern))
         {
             return(false);
         }
         if (CreateFormattingTemplate(numberFormat))
         {
             currentFormattingPattern          = pattern;
             shouldAddSpaceAfterNationalPrefix =
                 NationalPrefixSeparatorsPattern.Match(numberFormat.NationalPrefixFormattingRule).Success;
             // With a new formatting template, the matched position using the old template needs to be
             // reset.
             lastMatchPosition = 0;
             return(true);
         }
         // Remove the current number format from possibleFormats.
         possibleFormats.RemoveAt(0);
     }
     ableToFormat = false;
     return(false);
 }
        /**
         * Attempts to extract a match from {@code candidate} if the whole candidate does not qualify as a
         * match.
         *
         * @param candidate  the candidate text that might contain a phone number
         * @param offset  the current offset of {@code candidate} within {@link #text}
         * @return  the match found, null if none can be found
         */
        private PhoneNumberMatch ExtractInnerMatch(String candidate, int offset)
        {
            // Try removing either the first or last "group" in the number and see if this gives a result.
            // We consider white space to be a possible indications of the start or end of the phone number.
            var groupMatcher = GROUP_SEPARATOR.Match(candidate);

            if (groupMatcher.Success)
            {
                // Try the first group by itself.
                String firstGroupOnly = candidate.Substring(0, groupMatcher.Index);
                firstGroupOnly = TrimAfterUnwantedChars(firstGroupOnly);
                PhoneNumberMatch match = ParseAndVerify(firstGroupOnly, offset);
                if (match != null)
                {
                    return(match);
                }
                maxTries--;

                int withoutFirstGroupStart = groupMatcher.Index + groupMatcher.Length;
                // Try the rest of the candidate without the first group.
                String withoutFirstGroup = candidate.Substring(withoutFirstGroupStart);
                withoutFirstGroup = TrimAfterUnwantedChars(withoutFirstGroup);
                match             = ParseAndVerify(withoutFirstGroup, offset + withoutFirstGroupStart);
                if (match != null)
                {
                    return(match);
                }
                maxTries--;

                if (maxTries > 0)
                {
                    int lastGroupStart = withoutFirstGroupStart;
                    while ((groupMatcher = groupMatcher.NextMatch()).Success)
                    {
                        // Find the last group.
                        lastGroupStart = groupMatcher.Index;
                    }
                    String withoutLastGroup = candidate.Substring(0, lastGroupStart);
                    withoutLastGroup = TrimAfterUnwantedChars(withoutLastGroup);
                    if (withoutLastGroup.Equals(firstGroupOnly))
                    {
                        // If there are only two groups, then the group "without the last group" is the same as
                        // the first group. In these cases, we don't want to re-check the number group, so we exit
                        // already.
                        return(null);
                    }
                    match = ParseAndVerify(withoutLastGroup, offset);
                    if (match != null)
                    {
                        return(match);
                    }
                    maxTries--;
                }
            }
            return(null);
        }