@register.filter
@stringfilter
def clean_lccn(value):
    # remove all blanks
    value = value.replace(' ', '')
    # if there's a forward slash, remove it and all characters to its right
    if '/' in value:
        value = value[:value.index('/')]
    if '-' in value:
        # remove the hyphen
        left, sep, right = value.partition('-')
        # all chars in right should be digits, and len <= 6
        right = ''.join([c for c in right if c.isdigit()])
        if right:
            if len(right) > 6:
                return ''
            # left-pad with 0s until len == 6
            if len(right) < 6:
                right = '%06d' % int(right)
        value = '%s%s' % (left, right)
    if len(value) < 8 or len(value) > 12:
        return ''
    else:
        return value
Following the logic/examples described at:
dataset/ETHPy150Open gwu-libraries/launchpad/lp/ui/templatetags/launchpad_extras.py/clean_lccn