We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Love this challenge ! It was very fast for me to check the 5 first conditions in regex, but so complex to check the "Not 4 or more consecutive repeated digits" condition.
For those who come after, I think that a good tip is to create 2 regex patterns. Instead of doing 1 big pattern.
In my logic, I check all conditions except the first one. Because if already one of them is not valid, I can print "Invalid".
But if all conditions are valid, then I check the consecutive repeated digit condition.
For that, I firstly remove "-" if necessary, because it's obviously most complex if not.
Then I use the regex pattern "([0-9])\1{3,}".
I didn't know the "back reference" in regex (\1, \2 etc...).
\1 means : "re-use the last group"
So ([0-9])\1 means : I capture a digit. And I want this exact same digit after.
Obviously, by adding {3,} we want that this logic is applied 3 times or more.
And if this condition is True, it's Invalid, because we don't want to have 4 consecutives repeated digits.
Note: Be careful to use re.search() and not re.match() for this pattern. Because re.match() only search at the start, whereas re.search() search everywhere in the string.
Here my code :
importreN=int(input())pattern=r"(^([456])([0-9]{3})-([0-9]{4})-([0-9]{4})-([0-9]{4})$)|(^([456])([0-9]{15})$)"# Easier to have a second regex pattern only for the repeated consecutive digit conditionpattern_repeated_digit=r"([0-9])\1{3,}"for_inrange(N):current_credit_card=input()ifbool(re.match(pattern,current_credit_card)):# Now that we have check all conditions except repeated digits, we will check this last condition. Before, we have to remove '-'. However, it will be too complex.if'-'incurrent_credit_card:current_credit_card=current_credit_card.replace('-','')# If there is consecutive repeated digit : Invalid. Else : Valid.ifbool(re.search(pattern_repeated_digit,current_credit_card)):print("Invalid")else:print("Valid")else:print("Invalid")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Validating Credit Card Numbers
You are viewing a single comment's thread. Return to all comments →
Love this challenge ! It was very fast for me to check the 5 first conditions in regex, but so complex to check the "Not 4 or more consecutive repeated digits" condition.
For those who come after, I think that a good tip is to create 2 regex patterns. Instead of doing 1 big pattern.
In my logic, I check all conditions except the first one. Because if already one of them is not valid, I can print "Invalid".
But if all conditions are valid, then I check the consecutive repeated digit condition.
For that, I firstly remove "-" if necessary, because it's obviously most complex if not.
Then I use the regex pattern "([0-9])\1{3,}".
I didn't know the "back reference" in regex (\1, \2 etc...).
\1 means : "re-use the last group"
So ([0-9])\1 means : I capture a digit. And I want this exact same digit after.
Obviously, by adding {3,} we want that this logic is applied 3 times or more.
And if this condition is True, it's Invalid, because we don't want to have 4 consecutives repeated digits.
Note: Be careful to use re.search() and not re.match() for this pattern. Because re.match() only search at the start, whereas re.search() search everywhere in the string.
Here my code :