1 #include <reflex/pattern.h>
2 #include <reflex/matcher.h>
3 
4 static reflex::Pattern card_patterns(
5   "(?# MasterCard)(5[1-5]\\d{14})|"                    // 1st group = MC
6   "(?# Visa)(4\\d{12}(?:\\d{3})?)|"                    // 2nd group = VISA
7   "(?# AMEX)(3[47]\\d{13})|"                           // 3rd group = AMEX
8   "(?# Discover)(6011\\d{14})|"                        // 4th group = Discover
9   "(?# Diners Club)((?:30[0-5]|36\\d|38\\d)\\d{11})"); // 5th group = Diners
10 
11 static const char *card_data =
12   "mark 5212345678901234\n"
13   "vinny 4123456789012\n"
14   "victor 4123456789012345\n"
15   "amy 371234567890123\n"
16   "dirk 601112345678901234\n"
17   "doc 38812345678901 end\n";
18 
get_card_numbers(void)19 void get_card_numbers(void)
20 {
21   std::vector<std::string> cards[5];
22   std::cout << "Patterns (line,col);[first,last]:" << std::endl;
23 
24   // C++11 we can use a range based loop that is much simpler:
25   // for (auto& match : reflex::Matcher(card_patterns, card_data).find)
26   reflex::Matcher matcher(card_patterns, card_data);
27   for (reflex::Matcher::iterator match = matcher.find.begin(); match != matcher.find.end(); ++match)
28   {
29     cards[match->accept() - 1].push_back(match->text());
30     std::cout
31       << match->lineno()
32       << ","
33       << match->columno()
34       << ";["
35       << match->first()
36       << ","
37       << match->last()
38       << "]: "
39       << match->text()
40       << std::endl;
41   }
42 
43   std::cout << "Cards:" << std::endl;
44   for (int i = 0; i < 5; ++i)
45     for (std::vector<std::string>::const_iterator j = cards[i].begin(); j != cards[i].end(); ++j)
46       std::cout << i << ": " << *j << std::endl;
47   std::cout << "where\n0 = MasterCard\n1 = Visa\n2 = AMEX\n3 = Discover\n4 = Diners Club" << std::endl;
48 }
49 
main()50 int main()
51 {
52   get_card_numbers();
53   return EXIT_SUCCESS;
54 }
55 
56