How to validate MasterCard number using Regular Expression | C ++ program to validate Master Card number using Regular Expression ; Function to validate Master Card number ; Regex to check valid Master Card number ; If the Master Card number is empty return false ; Return true if the Master Card number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidMasterCardNo ( string str ) { const regex pattern ( " ^ 5[1-5 ] [ 0-9 ] { 14 } | ^ ( 222[1-9 ] ▁ 22[3-9 ] \\ d ▁ 2[3-6 ] \\ d { 2 } ▁ 27[0-1 ] \\ d ▁ 2720 ) [ 0-9 ] { 12 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "5114496353984312" ; cout << isValidMasterCardNo ( str1 ) << endl ; string str2 = "2720822463109651" ; cout << isValidMasterCardNo ( str2 ) << endl ; string str3 = "5582822410" ; cout << isValidMasterCardNo ( str3 ) << endl ; string str4 = "6082822463100051" ; cout << isValidMasterCardNo ( str4 ) << endl ; string str5 = "2221149a635 # # 843" ; cout << isValidMasterCardNo ( str5 ) << endl ; return 0 ; }