12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #include <iostream>
- #include <regex>
- #include <string>
- int main() {
- std::string input;
-
- std::regex integer("(\\+|-)?([[:digit:]]+)");
- std::smatch match;
-
- while (true) {
- std::cout << "Give me an integer! (q to quit)" << std::endl;
- std::getline(std::cin, input);
-
- if (!std::cin)
- break;
-
- if (input == "q")
- break;
-
- std::cout << "Length: " << input.length() << " Empty? " << input.empty() << std::endl;
- if (input.empty())
- break;
-
- std::vector<std::pair<int,int>> pos_len;
- for (auto it = std::sregex_iterator(input.begin(), input.end(), integer);
- it != std::sregex_iterator(); ++it) {
- pos_len.push_back(std::make_pair(it->position(), it->length() ) );
- std::cout << it->position() << " " << it->length() << " : " << it->str()
- << std::endl;
- }
- for (std::vector<std::pair<int,int>>::iterator vit = pos_len.begin(); vit != pos_len.end(); ++vit ) {
- std::pair<int,int> p;
- p = *vit;
- cout << (*vit).first << "," << (*vit).second << endl;
- cout << p.first << "," << p.second << endl;
- }
- #ifdef RX1
- if (std::regex_match(input, match, integer)) {
- std::cout << "integer" << std::endl;
- for (int i = 0; i < match.size(); ++i) {
- std::cout << "match " << i << " (" << match[i] << ") At "
- << match.position(i) << " of " << match.length(i) << " chars."
- << std::endl;
- }
- for (std::smatch::iterator im = match.begin(); im != match.end(); ++im) {
- std::cout << "matches:" << *im << std::endl;
- }
- } else {
- std::cout << "Invalid input" << std::endl;
- }
- int offset = 0;
- while (std::regex_search(input, match, integer)) {
- std::cout << "input: [" << input << "]" << std::endl;
- std::cout << offset << " match len " << match.length(0) << std::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." << std::endl;
- }
- for (auto x : match) {
- std::cout << x << " ";
- }
- std::cout << std::endl;
- input = match.suffix().str();
- offset += match.length(0) + match.prefix(0).size();
- }
- #endif
- }
- }
|