Home > Back-end >  python: regex match credit card starting with fixed letters
python: regex match credit card starting with fixed letters

Time:10-12

I want to write a regex that matches the following credit card that starts with fixed letters, e.g,

examples = ['AU01 0000 1234 5678 9012 00', 'AU01 3200 1564 5678 9987 55']

I have written the following regex but it does not capture the letters 'AU'.

regex = r'(?:(\w*AU)[0-9]{4}\s){3}[0-9]{4}|[0-9]{16}|[0-9]{2}'

CodePudding user response:

This expression will include the starting A and U once each and then look for 4 combinations of 4 digit numbers and then a combination of 2 digit number at the end: AU(\d{4}){4}(\d{2}).

NOTE : You'll need to remove the spaces from the strings in order to match this expression.

  • Related