]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/regex/example/snippets/credit_card_example.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / regex / example / snippets / credit_card_example.cpp
CommitLineData
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
7c673cae 19#include <boost/regex.hpp>
b32b8144 20#include <string>
7c673cae
FG
21
22bool 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
28const boost::regex e("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z");
29const std::string machine_format("\\1\\2\\3\\4");
30const std::string human_format("\\1-\\2-\\3-\\4");
31
32std::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
37std::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>
43using namespace std;
44
45int 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