| 1 | /* |
|---|
| 2 | * |
|---|
| 3 | * Copyright (c) 2003-2004 |
|---|
| 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 captures_example.cpp |
|---|
| 15 | * VERSION see <boost/version.hpp> |
|---|
| 16 | * DESCRIPTION: Demonstrate the behaviour of captures. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include <boost/regex.hpp> |
|---|
| 20 | #include <iostream> |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | void print_captures(const std::string& regx, const std::string& text) |
|---|
| 24 | { |
|---|
| 25 | boost::regex e(regx); |
|---|
| 26 | boost::smatch what; |
|---|
| 27 | std::cout << "Expression: \"" << regx << "\"\n"; |
|---|
| 28 | std::cout << "Text: \"" << text << "\"\n"; |
|---|
| 29 | if(boost::regex_match(text, what, e, boost::match_extra)) |
|---|
| 30 | { |
|---|
| 31 | unsigned i, j; |
|---|
| 32 | std::cout << "** Match found **\n Sub-Expressions:\n"; |
|---|
| 33 | for(i = 0; i < what.size(); ++i) |
|---|
| 34 | std::cout << " $" << i << " = \"" << what[i] << "\"\n"; |
|---|
| 35 | std::cout << " Captures:\n"; |
|---|
| 36 | for(i = 0; i < what.size(); ++i) |
|---|
| 37 | { |
|---|
| 38 | std::cout << " $" << i << " = {"; |
|---|
| 39 | for(j = 0; j < what.captures(i).size(); ++j) |
|---|
| 40 | { |
|---|
| 41 | if(j) |
|---|
| 42 | std::cout << ", "; |
|---|
| 43 | else |
|---|
| 44 | std::cout << " "; |
|---|
| 45 | std::cout << "\"" << what.captures(i)[j] << "\""; |
|---|
| 46 | } |
|---|
| 47 | std::cout << " }\n"; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | else |
|---|
| 51 | { |
|---|
| 52 | std::cout << "** No Match found **\n"; |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | int main(int , char* []) |
|---|
| 57 | { |
|---|
| 58 | print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee"); |
|---|
| 59 | print_captures("a(b+|((c)*))+d", "abd"); |
|---|
| 60 | print_captures("(.*)bar|(.*)bah", "abcbar"); |
|---|
| 61 | print_captures("(.*)bar|(.*)bah", "abcbah"); |
|---|
| 62 | print_captures("^(?:(\\w+)|(?>\\W+))*$", "now is the time for all good men to come to the aid of the party"); |
|---|
| 63 | print_captures("^(?>(\\w+)\\W*)*$", "now is the time for all good men to come to the aid of the party"); |
|---|
| 64 | print_captures("^(\\w+)\\W+(?>(\\w+)\\W+)*(\\w+)$", "now is the time for all good men to come to the aid of the party"); |
|---|
| 65 | print_captures("^(\\w+)\\W+(?>(\\w+)\\W+(?:(\\w+)\\W+){0,2})*(\\w+)$", "now is the time for all good men to come to the aid of the party"); |
|---|
| 66 | return 0; |
|---|
| 67 | } |
|---|
| 68 | |
|---|