Files
regex/test/quick.cpp

56 lines
1.6 KiB
C++
Raw Normal View History

2017-12-02 18:24:00 +02:00
// Copyright 1998-2002 John Maddock
// Copyright 2017 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
// See library home page at http://www.boost.org/libs/regex
#include <boost/regex.hpp>
2021-10-10 15:51:51 +01:00
#include <cassert>
2017-12-02 18:24:00 +02:00
#include <string>
bool validate_card_format(const std::string& s)
{
static const boost::regex e("(\\d{4}[- ]){3}\\d{4}");
return boost::regex_match(s, e);
}
2017-12-03 00:36:11 +02:00
const boost::regex card_rx("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z");
2017-12-02 18:24:00 +02:00
const std::string machine_format("\\1\\2\\3\\4");
const std::string human_format("\\1-\\2-\\3-\\4");
std::string machine_readable_card_number(const std::string& s)
{
2017-12-03 00:36:11 +02:00
return boost::regex_replace(s, card_rx, machine_format, boost::match_default | boost::format_sed);
2017-12-02 18:24:00 +02:00
}
std::string human_readable_card_number(const std::string& s)
{
2017-12-03 00:36:11 +02:00
return boost::regex_replace(s, card_rx, human_format, boost::match_default | boost::format_sed);
2017-12-02 18:24:00 +02:00
}
int main()
{
std::string s[ 4 ] = { "0000111122223333", "0000 1111 2222 3333", "0000-1111-2222-3333", "000-1111-2222-3333" };
2021-10-10 15:51:51 +01:00
assert(!validate_card_format(s[0]));
assert(machine_readable_card_number(s[0]) == s[0]);
assert(human_readable_card_number(s[0]) == s[2]);
2017-12-02 18:24:00 +02:00
2021-10-10 15:51:51 +01:00
assert(validate_card_format(s[1]));
assert(machine_readable_card_number(s[1]) == s[0]);
assert(human_readable_card_number(s[1]) == s[2]);
2017-12-02 18:24:00 +02:00
2021-10-10 15:51:51 +01:00
assert(validate_card_format(s[2]));
assert(machine_readable_card_number(s[2]) == s[0]);
assert(human_readable_card_number(s[2]) == s[2]);
2017-12-02 18:24:00 +02:00
2021-10-10 15:51:51 +01:00
assert(!validate_card_format(s[3]));
2017-12-02 18:24:00 +02:00
2021-10-10 15:51:51 +01:00
return 0;
2017-12-02 18:24:00 +02:00
}