// Example 1
#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main() {
  string input;
  // regex integer("(\\+|-)?[[:digit:]]+");
  regex integer("(\\+|-)?([[:digit:]]+)");
  smatch match;
  // As long as the input is correct ask for another number
  while (true) {
    cout << "Give me an integer! (q to quit)" << endl;
    getline(cin, input);
    // cin >> input;
    if (!cin)
      break;
    // Exit when the user inputs q
    if (input == "q")
      break;

    // Apparently, hitting enter, doesn't give you a blank input!  use getline.

    cout << "Length: " << input.length() << " Empty? " << input.empty() << endl;
    if (input.empty())
      break;

    // I need position and length.
    vector<pair<int,int>> pos_len;

    for (auto it = sregex_iterator(input.begin(), input.end(), integer);
         it != sregex_iterator(); ++it) {
	    pos_len.push_back(make_pair(it->position(), it->length() ) );
      cout << it->position() << " " << it->length() << " : " << it->str()
           << endl;
    }

    for (vector<pair<int,int>>::iterator vit = pos_len.begin(); vit != pos_len.end(); ++vit ) {
	    pair<int,int> p;
		p = *vit;
    cout << (*vit).first << "," << (*vit).second << endl;
    cout << p.first << "," << p.second << endl;
	}

#ifdef RX1
    if (regex_match(input, match, integer)) {  
      cout << "integer" << endl;
      for (int i = 0; i < match.size(); ++i) {
        std::cout << "match " << i << " (" << match[i] << ") At "
                  << match.position(i) << " of " << match.length(i) << " chars."
                  << endl;
      }
      for (smatch::iterator im = match.begin(); im != match.end(); ++im) {
        cout << "matches:" << *im << std::endl;
      }
    } else {
      cout << "Invalid input" << endl;
    }

    int offset = 0;
    while (regex_search(input, match, integer)) {
      cout << "input: [" << input << "]" << endl;
      cout << offset << " match len " << match.length(0) << endl;
      for (int i = 0; i < match.size(); ++i) {
        std::cout << "match " << i << " (" << match[i] << ") At "
                  << offset + match.position(i) << " of " << match.length(i)
                  << " chars." << endl;
      }

      for (auto x : match) {
        cout << x << " ";
      }
      cout << endl;
      input = match.suffix().str();
      offset += match.length(0) + match.prefix(0).size();
    }
#endif
  }
}