1

For my c++ class, we have been given a "simple regular expression assignment". But every time I try to use regex_search() in Visual Studio Community 2015, I get "no instance of overloaded function "regex_search" matches the argument list". I mouse-over the error, and it tells me it wants (for one example) string, smatch, string; which is exactly what I give it.

Here is my code:

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

using namespace std;

int main( int argc, char *argv[] )
{
    string regexCriteria1 = "\\.$";
    string test = "asdf.";
    smatch searchResult;

    for each ( string line in quotes ) // I have also tried const auto &line in quotes
    {
        cout << regex_search( line, regexCriteria1 ) << endl;
        regex_search( line, searchResult, regexCriteria1 );
        regex_search( line.begin(), line.end(), searchResult, regexCriteria1 );
    }
    regex_search( test, searchResult, regexCriteria1 );
}

I have no idea why none of these regex_search() lines are working. This was supposed to be a 45 minute project, it has already taken me over 2 hours, and I am nowhere near done. This would have only taken me 5 minute in Java. Any help would be greatly appreciated. My wife is starting to fear for my sanity.

Adam Howell
  • 415
  • 1
  • 11
  • 24
  • 2
    Try again, but this time post your actual code. What is `for each ( string line in quotes )` supposed to mean? – user657267 Apr 08 '16 at 01:35
  • 1
    If you post questions about build errors, please include the *complete* build error, in unmodified form. The compiler will tell you what it tries to find, and what it have and expected, and that information is needed by us too if you want our help. – Some programmer dude Apr 08 '16 at 01:37
  • 2
    `regexCriteria1` should be a `std::regex`, not a `std::string`. – O'Neil Apr 08 '16 at 01:38
  • @user657267 Sorry, I cleaned up my code a bit to simplify, and forgot that my array was still referenced. It was an array of 8 strings, and it took up about a page of vertical space on this page. – Adam Howell Apr 08 '16 at 01:59
  • @AdamHowell It's good that you tried to make a minimal example, but it's important that the example is actually compilable, `for each` isn't valid c++. – user657267 Apr 08 '16 at 02:02
  • 2
    @user657267 `for each` is a non-standard Microsoft extension similar to range based loops – Ryan Pendleton Apr 08 '16 at 05:55
  • @RyanPendleton Good to know, I suppose it's deprecated now that range for is a thing? – user657267 Apr 08 '16 at 05:57
  • @user657267 That's correct. The MSDN recommends using range based loops instead. – Ryan Pendleton Apr 08 '16 at 05:58

2 Answers2

5

std::regex_search requires the search pattern to be specified as a std::regex, but you're passing a raw string.

Try:

std::regex_search( line, searchResult, std::regex(regexCriteria1) );

P.S. "using namespace std;" is a bad practice.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • That works, but why? The mouse-over says it wants (std::string, std::smatch, std::string). – Adam Howell Apr 08 '16 at 01:46
  • 3
    Don't trust what the mouse says. The ultimate judge and jury is the C++ standard, and the C++ library. – Sam Varshavchik Apr 08 '16 at 01:50
  • Thanks, Sam. I will try to get into the habit of avoiding "using namespace std;". It's just that it saves so much typing, and makes the code look cleaner. This is the type of thing that won't really sink in until it bites me, like your link pointed out. – Adam Howell Apr 08 '16 at 01:56
1

Your call in cout << regex_search( line, regexCriteria1 ) should be cout << regex_search( line, searchResult, regexCriteria1 )

And your declaration of regexCriteria1 is not correct. You should declare it like regex regexCriteria1("\\.$");

Shiv
  • 1,912
  • 1
  • 15
  • 21
  • That declaration change fixed almost all of them. Why did the mouse-over says it wants (std::string, std::smatch, std::string) when it wanted a regex instead of a string? – Adam Howell Apr 08 '16 at 01:50
  • 1
    http://en.cppreference.com/w/cpp/regex/regex_search please refer to this. Fundamentally, a regex is a string though that is slightly a wrong statement. – Shiv Apr 08 '16 at 01:52
  • 1
    But your IDE is wrong in saying that it is std::string rather mouse over should say std::regex. – Shiv Apr 08 '16 at 01:53
  • Thanks, Shiv. Things like this make me want to boot into Linux and code from there. – Adam Howell Apr 08 '16 at 01:57
  • 1
    You are welcome and yes you should use GNU/Linux. :) – Shiv Apr 08 '16 at 01:58