VS 2012 中的简单 regex_match 不起作用
simple regex_match in VS 2012 not working
我正在使用 VS 2012 C++。以下简单的 regex_match 永远找不到我要找的东西。我已将其简化为以下内容。我错过了什么?
#include <string>
#include <regex>
using namespace std;
int _tmain( int argc, char *argv[] )
{
int i = 0;
auto matches = smatch();
regex rx( "." );
string haystack( "ABC." );
if( regex_match( haystack, matches, rx ) )
i++;
if( regex_match( haystack, rx ) )
i++;
if( regex_match( haystack.begin(), haystack.end(), rx ) )
i++;
}
regex_match
总是 returns 错误。
你应该使用 regex_search.
Note that regex_match will only successfully match a regular
expression to an entire character sequence, whereas std::regex_search
will successfully match subsequences.
因此,此代码将找到匹配项:
if( regex_search( haystack, matches, rx ) )
i++;
if( regex_search( haystack, rx ) )
i++;
if( regex_search( haystack.begin(), haystack.end(), rx ) )
i++;
我正在使用 VS 2012 C++。以下简单的 regex_match 永远找不到我要找的东西。我已将其简化为以下内容。我错过了什么?
#include <string>
#include <regex>
using namespace std;
int _tmain( int argc, char *argv[] )
{
int i = 0;
auto matches = smatch();
regex rx( "." );
string haystack( "ABC." );
if( regex_match( haystack, matches, rx ) )
i++;
if( regex_match( haystack, rx ) )
i++;
if( regex_match( haystack.begin(), haystack.end(), rx ) )
i++;
}
regex_match
总是 returns 错误。
你应该使用 regex_search.
Note that regex_match will only successfully match a regular expression to an entire character sequence, whereas std::regex_search will successfully match subsequences.
因此,此代码将找到匹配项:
if( regex_search( haystack, matches, rx ) )
i++;
if( regex_search( haystack, rx ) )
i++;
if( regex_search( haystack.begin(), haystack.end(), rx ) )
i++;