Validating Credit Card Numbers

  • + 1 comment
    import sys, re
    
    text = sys.stdin.read().splitlines()[1:]
    format_pattern = r"^[456]\d{3}([-]?\d{4}){3}$"
    match = []
    
    for line in text:
        format_match = re.search(format_pattern, line)
    
        if format_match != None:
            occurence_match = re.search(r"(.)\1{3}", ''.join(re.split('-', line)))
            
            if occurence_match == None:
                match += ['Valid']
            else:
                match += ['Invalid']
        else:
            match += ['Invalid']
    
    for _ in match:
        print(_)