]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * | |
3 | * Copyright (c) 1998-2002 | |
4 | * John Maddock | |
5 | * | |
6 | * Use, modification and distribution are subject to the | |
7 | * Boost Software License, Version 1.0. (See accompanying file | |
8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | * | |
10 | */ | |
11 | ||
12 | /* | |
13 | * LOCATION: see http://www.boost.org for most recent version. | |
14 | * FILE credit_card_example.cpp | |
15 | * VERSION see <boost/version.hpp> | |
16 | * DESCRIPTION: Credit card number formatting code. | |
17 | */ | |
18 | ||
19 | #include <string> | |
20 | #include <boost/regex.hpp> | |
21 | ||
22 | bool validate_card_format(const std::string& s) | |
23 | { | |
24 | static const boost::regex e("(\\d{4}[- ]){3}\\d{4}"); | |
25 | return boost::regex_match(s, e); | |
26 | } | |
27 | ||
28 | const boost::regex e("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"); | |
29 | const std::string machine_format("\\1\\2\\3\\4"); | |
30 | const std::string human_format("\\1-\\2-\\3-\\4"); | |
31 | ||
32 | std::string machine_readable_card_number(const std::string& s) | |
33 | { | |
34 | return boost::regex_replace(s, e, machine_format, boost::match_default | boost::format_sed); | |
35 | } | |
36 | ||
37 | std::string human_readable_card_number(const std::string& s) | |
38 | { | |
39 | return boost::regex_replace(s, e, human_format, boost::match_default | boost::format_sed); | |
40 | } | |
41 | ||
42 | #include <iostream> | |
43 | using namespace std; | |
44 | ||
45 | int main() | |
46 | { | |
47 | string s[4] = { "0000111122223333", "0000 1111 2222 3333", | |
48 | "0000-1111-2222-3333", "000-1111-2222-3333", }; | |
49 | int i; | |
50 | for(i = 0; i < 4; ++i) | |
51 | { | |
52 | cout << "validate_card_format(\"" << s[i] << "\") returned " << validate_card_format(s[i]) << endl; | |
53 | } | |
54 | for(i = 0; i < 4; ++i) | |
55 | { | |
56 | cout << "machine_readable_card_number(\"" << s[i] << "\") returned " << machine_readable_card_number(s[i]) << endl; | |
57 | } | |
58 | for(i = 0; i < 4; ++i) | |
59 | { | |
60 | cout << "human_readable_card_number(\"" << s[i] << "\") returned " << human_readable_card_number(s[i]) << endl; | |
61 | } | |
62 | return 0; | |
63 | } | |
64 | ||
65 | ||
66 | ||
67 |