How to determine the type of credit card?

For a school assignment I'm trying to determine the type of credit card a user has when they type in their card number. A master card starts with 51-55. Extra zeros in front of that number would be considered valid. I've reviewed other threads on how to integrate this into python and although I could just copy and paste the code I still don't really understand whats going on or how they bypass the zeroes in front?

Heres an example of the Master card code to do this:

^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$ 

Could anyone explain to me what is going on? Thank you for your time.


What you posted is regex, not python.

Intro:

Regex is a sequence of characters that specify a search patter. The string can be used by various searching algorithms to perform find, find and replace, or match operations on strings.

Major languages all have ways to evaluate regex. For python see: https://docs.pytho.org/3/howto/regex.html

Understanding that regex

Some basic regex rules that would help someone understand the given pattern:

[] marks a list of characters, one of which must be present in that position for a match to be made. Ex: 'asdf[a-fz]' would match 'asdfe' or 'asdfz' but not 'asdfg'

The pipe "|" marks as others said "a branch". It basically means "or". Ex: 'I (am|was) happy' would match 'I am happy' and 'I was happy' but not 'I am not happy' or "I be happy".

Lastly {} is a quantifier. It means, "there must be between X and Y occurances of the previous character". So "[0-9]{2,4}" would match "12" and "1234" but not "1", "12345", or "cats". If you don't specify the right number, ex {3,} it means there's no limit. So "[0-9]{3,}" would match "12341234123412341234123412341234".

So the expression is basically intended to check a lot of things, among which, that the number starts with 5, 222, 22, 2, 27, or 2720. As stated in comments it doesn't properly check that it "starts" with those numbers because there isn't a ^ after each "|"(or), so it just says "Does it start with 5, or contain 222, 22, 2, 27, or 2720.".

Results of expression:

See: http://regexr.com/3h78i Or see: 给定表达式的结果

Correction

This expression has many fewer false positives:

^(5[1-5][0-9]{5,5}|222[1-9][0-9]{3,3}|22[3-9][0-9]{4,4}|2[3-6][0-9]{5,5}|27[01][0-9]{4,4}|2720[0-9]{3,3})d{9}$

See: http://regexr.com/3h78l or: 更好的正则表达式查询

Edit: My correction assumes credit cards are 16 digits. If this is a problem you can fix this by changing the {}'s back, and removing the d{9} from the end. Or just by changing d{9} to d+.

Alternative

My personal choice of method for determining credit card type is pycard: https://github.com/orokusaki/pycard

This is one of those things where I'd rather not personally be responsible for maintaining a list of rules that may potentially change and ensuring accurate detection. The linked library is open source, and the guy who made it has been has been active in merging pull requests made by people suggesting improvements to the library up until the time or writing. If it ever were to become stale and you needed a change you could just fork it.

链接地址: http://www.djcxy.com/p/70736.html

上一篇: 通过iPhone接受信用卡付款有哪些选择?

下一篇: 如何确定信用卡的类型?