

def parse(number, region=None, keep_raw_input=False, numobj=None, _check_region=True):
    'Parse a string and return a corresponding PhoneNumber object.\n\n    The method is quite lenient and looks for a number in the input text\n    (raw input) and does not check whether the string is definitely only a\n    phone number. To do this, it ignores punctuation and white-space, as\n    well as any text before the number (e.g. a leading "Tel: ") and trims\n    the non-number bits.  It will accept a number in any format (E164,\n    national, international etc), assuming it can be interpreted with the\n    defaultRegion supplied. It also attempts to convert any alpha characters\n    into digits if it thinks this is a vanity number of the type "1800\n    MICROSOFT".\n\n    This method will throw a NumberParseException if the number is not\n    considered to be a possible number. Note that validation of whether the\n    number is actually a valid number for a particular region is not\n    performed. This can be done separately with is_valid_number.\n\n    Arguments:\n    number -- The number that we are attempting to parse. This can\n              contain formatting such as +, ( and -, as well as a phone\n              number extension. It can also be provided in RFC3966 format.\n    region -- The region that we are expecting the number to be from. This\n              is only used if the number being parsed is not written in\n              international format. The country_code for the number in\n              this case would be stored as that of the default region\n              supplied. If the number is guaranteed to start with a \'+\'\n              followed by the country calling code, then None or\n              UNKNOWN_REGION can be supplied.\n    keep_raw_input -- Whether to populate the raw_input field of the\n              PhoneNumber object with number (as well as the\n              country_code_source field).\n    numobj -- An optional existing PhoneNumber object to receive the\n              parsing results\n    _check_region -- Whether to check the supplied region parameter;\n              should always be True for external callers.\n\n    Returns a PhoneNumber object filled with the parse number.\n\n    Raises:\n    NumberParseException if the string is not considered to be a viable\n    phone number (e.g.  too few or too many digits) or if no default\n    region was supplied and the number is not in international format\n    (does not start with +).\n    '
    if (numobj is None):
        numobj = PhoneNumber()
    if (number is None):
        raise NumberParseException(NumberParseException.NOT_A_NUMBER, 'The phone number supplied was None.')
    elif (len(number) > _MAX_INPUT_STRING_LENGTH):
        raise NumberParseException(NumberParseException.TOO_LONG, 'The string supplied was too long to parse.')
    national_number = _build_national_number_for_parsing(number)
    if (not _is_viable_phone_number(national_number)):
        raise NumberParseException(NumberParseException.NOT_A_NUMBER, 'The string supplied did not seem to be a phone number.')
    if (_check_region and (not _check_region_for_parsing(national_number, region))):
        raise NumberParseException(NumberParseException.INVALID_COUNTRY_CODE, 'Missing or invalid default region.')
    if keep_raw_input:
        numobj.raw_input = number
    (extension, national_number) = _maybe_strip_extension(national_number)
    if (len(extension) > 0):
        numobj.extension = extension
    if (region is None):
        metadata = None
    else:
        metadata = PhoneMetadata.metadata_for_region(region.upper(), None)
    country_code = 0
    try:
        (country_code, normalized_national_number) = _maybe_extract_country_code(national_number, metadata, keep_raw_input, numobj)
    except NumberParseException:
        (_, e, _) = sys.exc_info()
        matchobj = _PLUS_CHARS_PATTERN.match(national_number)
        if ((e.error_type == NumberParseException.INVALID_COUNTRY_CODE) and (matchobj is not None)):
            (country_code, normalized_national_number) = _maybe_extract_country_code(national_number[matchobj.end():], metadata, keep_raw_input, numobj)
            if (country_code == 0):
                raise NumberParseException(NumberParseException.INVALID_COUNTRY_CODE, 'Could not interpret numbers after plus-sign.')
        else:
            raise
    if (country_code != 0):
        number_region = region_code_for_country_code(country_code)
        if (number_region != region):
            metadata = PhoneMetadata.metadata_for_region_or_calling_code(country_code, number_region)
    else:
        national_number = _normalize(number_region)
        normalized_national_number += national_number
        if (region is not None):
            country_code = metadata.country_code
            numobj.country_code = country_code
        elif keep_raw_input:
            numobj.country_code_source = None
    if (len(normalized_national_number) < _MIN_LENGTH_FOR_NSN):
        raise NumberParseException(NumberParseException.TOO_SHORT_NSN, 'The string supplied is too short to be a phone number.')
    if (metadata is not None):
        potential_national_number = normalized_national_number
        (carrier_code, potential_national_number, _) = _maybe_strip_national_prefix_carrier_code(potential_national_number, metadata)
        if (not _is_shorter_than_possible_normal_number(metadata, potential_national_number)):
            normalized_national_number = potential_national_number
            if keep_raw_input:
                numobj.preferred_domestic_carrier_code = carrier_code
    len_national_number = len(normalized_national_number)
    if (len_national_number < _MIN_LENGTH_FOR_NSN):
        raise NumberParseException(NumberParseException.TOO_SHORT_NSN, 'The string supplied is too short to be a phone number.')
    if (len_national_number > _MAX_LENGTH_FOR_NSN):
        raise NumberParseException(NumberParseException.TOO_LONG, 'The string supplied is too long to be a phone number.')
    _set_italian_leading_zeros_for_phone_number(normalized_national_number, numobj)
    numobj.national_number = to_long(normalized_national_number)
    return numobj

dataset/ETHPy150Open daviddrysdale/python-phonenumbers/python/phonenumbers/phonenumberutil.py parse/VarMisuse@396/1558 `national_number`->`number_region`