vendredi 14 décembre 2018

C++11 trying to extract a substring using regex

This is my application:

// regex_example.cpp:
//  How to compile:
//   $ g++ -std=c++11 regex_example.cpp -o regex_example

#include <iostream>
#include <string>
#include <regex>

int main()
{
  std::string input = "Pizza Carrot Lasagna 15000  ";
  std::smatch match;
  std::regex test_reg("[^0-9]*([0-9]+).*");

  if (std::regex_search(input.begin(), input.end(), match, test_reg))
  {
    std::cout << "This is the string found: " << match[1] << std::endl;
  }

  return 0;
}

When I compile it, this is what the compiler shows me:

regex_example.cpp: In function ‘int main()’: regex_example.cpp:24:68: error: no matching function for call to ‘regex_search(std::__cxx11::basic_string::iterator, std::__cxx11::basic_string::iterator, std::__cxx11::smatch&, std::__cxx11::regex&)’ if (std::regex_search(input.begin(), input.end(), match, test_reg))

Basically, I'm trying to do the following:

1 - Have this compile. I don't understand why I'm getting the syntax error.

2 - From the input string, I'm trying to extract the number 15000. I'm assuming that when I get this compiling, I will get a string of 15000.

Aucun commentaire:

Enregistrer un commentaire