Saltar al contenido

ejemplo de código python de patrón de regex de tarjeta de crédito

Este equipo redactor ha pasado horas buscando para darle espuestas a tus dudas, te compartimos la solución por esto deseamos servirte de mucha apoyo.

Ejemplo 1: dividir el número de la tarjeta de crédito python

import re
defcc(pat):# check for the pattern #### #### #### #### with each '#' being a digit
    m=re.match(r'(d4)s(d4)s(d4)s(d4)$', pat.strip())ifnot m:returnFalse# join all the digits in the 4 groups matched, # turn into a list of ints, # sum and # return True/False if divisible by 10: returnsum(int(c)for c in''.join(m.groups()))%10==0>>> cc('9384 3495 3297 0123')False>>> cc('9384 3495 3297 0121')True

Ejemplo 2: dividir el número de la tarjeta de crédito python

defCardNumber():global card     #  Making variable global for function SumCardNumDigits(); see below
        card =input()# Credit card number is enteredreturn card


    defcheck(S):
        CardNumber = S
        SplitCardNum = CardNumber.split()# Split credit card number into a list, so we get [####, ####, ####, ####]for i inrange(0,len(SplitCardNum)):# Checking if each item in list has length of four strings and each item is# an actual a numberiflen(SplitCardNum[i])==4and SplitCardNum[i].isdigit():
            SplitCardNum.insert(i,True)# We add to a list a True value at position idel SplitCardNum[i +1]# We delete items at position i + 1return SplitCardNum


    defcheckFormat(SplitCardNum):if SplitCardNum ==[True]*4:# Checking if all above conditions are met in function check(S)# So the returned value from previous function is a list [True, True, True, True]returnTrueelse:returnFalsedefSumCardNumDigits():
        Ncard = card                    # Using global variable 'card' from function CardNumber()
        SumN =0for i in Ncard:# Summing up all digits in string 'Ncard', if i position in a string is empty space " "# we skip a step.if i ==" ":continueelse:
                SumN +=int(i)return SumN


    defDivideByTen(SplitCardNum):if SplitCardNum ==True:# If conditions from function check(S) are met, we divide the sum of digits# of credit card number by 10
            SumNumber = SumCardNumDigits()%10# We do this by using the returned value of function SumCardNumDigits()global SumNumber       # <--- Fixed codereturn SumNumber
        else:returnFalsedefIsDivideByTen(SumNumber):
        check = checkFormat(SplitCardNum)# Finally we check if the sum of digits of credit card number is divisible by 10if SumNumber ==0and check ==True:# <--- Fixed codereturnTrueelse:returnFalseprint(IsDivideByTen(DivideByTen(checkFormat(check(CardNumber())))))# Output final result# You can test this code at: http://cscircles.cemc.uwaterloo.ca/visualize/#mode=edit  and see how it works.# Try for '9384 3495 3297 4523' and '9384 3495 3297 4526'

Ten en cuenta dar difusión a este escrito si si solucionó tu problema.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *