]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/credit_card_example.cpp
3 * Copyright (c) 1998-2002
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)
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.
20 #include <boost/regex.hpp>
22 bool validate_card_format(const std::string
& s
)
24 static const boost::regex
e("(\\d{4}[- ]){3}\\d{4}");
25 return boost::regex_match(s
, e
);
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");
32 std::string
machine_readable_card_number(const std::string
& s
)
34 return boost::regex_replace(s
, e
, machine_format
, boost::match_default
| boost::format_sed
);
37 std::string
human_readable_card_number(const std::string
& s
)
39 return boost::regex_replace(s
, e
, human_format
, boost::match_default
| boost::format_sed
);
47 string s
[4] = { "0000111122223333", "0000 1111 2222 3333",
48 "0000-1111-2222-3333", "000-1111-2222-3333", };
50 for(i
= 0; i
< 4; ++i
)
52 cout
<< "validate_card_format(\"" << s
[i
] << "\") returned " << validate_card_format(s
[i
]) << endl
;
54 for(i
= 0; i
< 4; ++i
)
56 cout
<< "machine_readable_card_number(\"" << s
[i
] << "\") returned " << machine_readable_card_number(s
[i
]) << endl
;
58 for(i
= 0; i
< 4; ++i
)
60 cout
<< "human_readable_card_number(\"" << s
[i
] << "\") returned " << human_readable_card_number(s
[i
]) << endl
;