#include <iostream>
#include <regex>
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
  
  match();
  match2();

  search();
  search2();

  replace();
  replace2();

  Serial.printf("provola %d\n", provola("Hello, World!", "Hello, (\\w+)"));
}

void loop() {
  return;
  // put your main code here, to run repeatedly:
  delay(1000); // this speeds up the simulation

  Serial.printf("ramfree %d\n", ESP.getFreeHeap());
  // Release memory allocated by matches
  //sm = std::smatch();
  Serial.printf("ramfree %d\n", ESP.getFreeHeap());



  
}


int match() {
    std::string s = "Hello, World!";
    std::regex pattern("Hello, (\\w+)!");

    if (std::regex_match(s, pattern)) {
        std::cout << "Entire string matches the pattern" << std::endl;
    } else {
        std::cout << "Entire string doesn't match the pattern" << std::endl;
    }

    return 0;
}

int match2() {
    std::string s = "Hello, World!";
    std::regex pattern("\\b(Hello), (\\w+)!");

    std::smatch matches;
    if (std::regex_match(s, matches, pattern)) {
        std::cout << "Entire string matches the pattern" << std::endl;
        for (size_t i = 0; i < matches.size(); ++i) {
            std::cout << "Match " << i << ": " << matches[i] << std::endl;
        }
    } else {
        std::cout << "Entire string doesn't match the pattern" << std::endl;
    }

    return 0;
}

int search() {
    std::string s = "Hello, World!";
    std::regex pattern("\\b\\w+\\b");

    std::smatch matches;
    if (std::regex_search(s, matches, pattern)) {
        std::cout << "Found match: " << matches[0] << std::endl;
    } else {
        std::cout << "No match found" << std::endl;
    }

    return 0;
}

int search2() {
    std::string s = "Hello, World! How are you?";
    std::regex pattern("\\b\\w+\\b");

    std::smatch matches;
    while (std::regex_search(s, matches, pattern)) {
        for (size_t i = 0; i < matches.size(); ++i) {
            std::cout << "Match " << i << ": " << matches[i] << std::endl;
        }
        s = matches.suffix().str(); // Update the string to search after the last match
    }

    return 0;
}

int replace() {
    std::string s = "Hello, World!";
    std::regex pattern("\\b(\\w+)\\b");

    //std::string result = std::regex_replace(s, pattern, "Goodbye");
    std::string result = std::regex_replace(s, pattern, "Goodbye", std::regex_constants::format_first_only);
    std::cout << "Result: " << result << std::endl;

    return 0;
}

int replace2() {
    std::string s = "Hello, World! How are you?";
    std::regex pattern("\\b\\w+\\b");

    std::string result = std::regex_replace(s, pattern, "Replacement");

    std::cout << "Result: " << result << std::endl;

    return 0;
}

bool provola(String str, String pattern) {
  std::regex patt(pattern.c_str());
      if (std::regex_match(str.c_str(), patt)) {
        std::cout << "match OK" << std::endl;
    } else {
        std::cout << "no match" << std::endl;
    }
  return false;
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK