throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
"Country code supplied was not recognised.");
} else if (defaultRegionMetadata != null) {
// Check to see if the number is valid for the default region already. If not, we check to
// see if the country code for the default region is present at the start of the number.
PhoneNumberDesc generalDesc = defaultRegionMetadata.getGeneralDesc();
Pattern validNumberPattern =
regexCache.getPatternForRegex(generalDesc.getNationalNumberPattern());
if (!validNumberPattern.matcher(fullNumber).matches()) {
int defaultCountryCode = defaultRegionMetadata.getCountryCode();
String defaultCountryCodeString = String.valueOf(defaultCountryCode);
String normalizedNumber = fullNumber.toString();
if (normalizedNumber.startsWith(defaultCountryCodeString)) {
// If so, strip this, and see if the resultant number is valid.
StringBuffer potentialNationalNumber =
new StringBuffer(normalizedNumber.substring(defaultCountryCodeString.length()));
maybeStripNationalPrefix(
potentialNationalNumber,
defaultRegionMetadata.getNationalPrefixForParsing(),
defaultRegionMetadata.getNationalPrefixTransformRule(),
validNumberPattern);
Matcher possibleNumberMatcher =
regexCache.getPatternForRegex(generalDesc.getPossibleNumberPattern()).matcher(
potentialNationalNumber);
// If the resultant number is either valid, or still too long even with the country code
// stripped, we consider this a better result and keep the potential national number.
if (validNumberPattern.matcher(potentialNationalNumber).matches() ||
(possibleNumberMatcher.lookingAt() &&