1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional Information Technology-Programming Book part 120 pot

5 84 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

All Visa numbers start with 4 and are 13 or 16 digits (but not 14 or 15, and so a range cannot be used). 4 matches 4, \d{12} matches the next 12 digits, and (\d{3})? matches an additional 3 digits if they are present. American Express requires a much simpler pattern. MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 3[47]\d{13} MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 American Express numbers are 15 digits and start with 34 or 37. 3[47] matches the first 2 digits, and \d{13} matches the remaining 13 digits. Discover uses a simple pattern. MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 6011\d{14} MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 All Discover cards are 16 digits and start with digits 6011; 6011\d{14} does the trick. Diners Club is a little trickier. MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 (30[0-5]|36\d|38\d)\d{11} MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 Diners Club numbers are 14 digits and begin with 300 through 305, 36, or 38. If the opening digits are 300 through 305, an additional 11 digits are needed, whereas if the opening digits are 36 or 38, an additional 12 digits are needed. To make this simpler, the pattern first matches the first three digits regardless of what they are. (30[0-5]|36\d|38\d) has three expressions, any of which must match; 30[0-5] matches 300 through 305, 36\d matches any three-digit number starting with 36, and 38\d matches any three-digit number starting with 38. This way, \d{11} can be used to match the remaining 11 digits. All that remains now is a way to check any of the five card types used here. MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 (5[1-5]\d{14})|(4\d{12}(\d{3})?)|(3[47]\d{13})|(6011\d{14})|((30[0- 5]|36\d|38\d)\d{11}) MasterCard: 5212345678901234 Visa 1: 4123456789012 Visa 2: 4123456789012345 Amex: 371234567890123 Discover: 601112345678901234 Diners Club: 38812345678901 The pattern here uses alternation (providing alternatives or statements) to include all the previous patterns, each separated by a |. The result? Simple validation of all major credit card types. Note The patterns used here ensure that the credit card number has the correct opening digits and is of the correct length. However, not every 13- digit number that begins with 4 is a valid Visa number. A formula known as Mod 10 can be used to perform a calculation on the digits (of all the credit card types mentioned here) so as to determine if the digits are truly valid. Mod 10 is an important part of implementing credit card processing, but it is not a job for regular expressions because it involves performing mathematical calculations. Summary You have now seen practical examples of many of the concepts and ideas introduced in prior lessons. Feel free to use, and adapt, any of the examples used here; with that, welcome to the exciting and productive world of regular expressions. Appendix C. The Regular Expression Tester Testing and experimenting with regular expressions requires the use of an application or language (and possibly the writing of code, too). To help you learn and test regular expressions, you may find it useful to download and use the standalone Regular Expression Tester application that was created specifically for this book. This appendix briefly describes this application. Understanding the Regular Expression Tester Application . types mentioned here) so as to determine if the digits are truly valid. Mod 10 is an important part of implementing credit card processing, but it is not a job for regular expressions because. use the standalone Regular Expression Tester application that was created specifically for this book. This appendix briefly describes this application. Understanding the Regular Expression Tester

Ngày đăng: 07/07/2014, 03:20

Xem thêm: Professional Information Technology-Programming Book part 120 pot