• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         partial_regex_match.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: regex_match example using partial matches.
17   */
18 
19 #include <boost/regex.hpp>
20 #include <string>
21 #include <iostream>
22 
23 boost::regex e("(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})");
24 
is_possible_card_number(const std::string & input)25 bool is_possible_card_number(const std::string& input)
26 {
27    //
28    // return false for partial match, true for full match, or throw for
29    // impossible match based on what we have so far...
30    boost::match_results<std::string::const_iterator> what;
31    if(0 == boost::regex_match(input, what, e, boost::match_default | boost::match_partial))
32    {
33       // the input so far could not possibly be valid so reject it:
34       throw std::runtime_error("Invalid data entered - this could not possibly be a valid card number");
35    }
36    // OK so far so good, but have we finished?
37    if(what[0].matched)
38    {
39       // excellent, we have a result:
40       return true;
41    }
42    // what we have so far is only a partial match...
43    return false;
44 }
45 
main(int argc,char * argv[])46 int main(int argc, char* argv[])
47 {
48    try{
49       std::string input;
50       if(argc > 1)
51          input = argv[1];
52       else
53          std::cin >> input;
54       if(is_possible_card_number(input))
55       {
56          std::cout << "Matched OK..." << std::endl;
57       }
58       else
59          std::cout << "Got a partial match..." << std::endl;
60    }
61    catch(const std::exception& e)
62    {
63       std::cout << e.what() << std::endl;
64    }
65    return 0;
66 }
67 
68 
69 
70