问题描述:

I am trying to match a literal number, e.g. 1600442 using a set of regular expressions in Microsoft Visual Studio 2010. My regular expressions are simply:

1600442|7654321

7895432

The problem is that both of the above matches the string.

Implementing this in Python gives the expected result:

import re

serial = "1600442"

re1 = "1600442|7654321"

re2 = "7895432"

m = re.match(re1, serial)

if m:

print "found for re1"

print m.groups()

m = re.match(re2, serial)

if m:

print "found for re2"

print m.groups()

Gives output

found for re1

()

Which is what I expected. Using this code in C++ however:

#include <string>

#include <iostream>

#include <regex>

int main(){

std::string serial = "1600442";

std::tr1::regex re1("1600442|7654321");

std::tr1::regex re2("7895432");

std::tr1::smatch match;

std::cout << "re1:" << std::endl;

std::tr1::regex_search(serial, match, re1);

for (auto i = 0;i <match.length(); ++i)

std::cout << match[i].str().c_str() << " ";

std::cout << std::endl << "re2:" << std::endl;

std::tr1::regex_search(serial, match, re2);

for (auto i = 0;i <match.length(); ++i)

std::cout << match[i].str().c_str() << " ";

std::cout << std::endl;

std::string s;

std::getline (std::cin,s);

}

gives me:

re1:

1600442

re2:

1600442

which is not what I expected. Why do I get match here?

网友答案:

The smatch does not get overwritten by the second call to regex_search thus, it is left intact and contains the first results.

You can move the regex searching code to a separate method:

void FindMeText(std::regex re, std::string serial) 
{
    std::smatch match;
    std::regex_search(serial, match, re);
    for (auto i = 0;i <match.length(); ++i)
            std::cout << match[i].str().c_str() << " ";
    std::cout << std::endl;
}

int main(){
    std::string serial = "1600442";
    std::regex re1("^(?:1600442|7654321)");
    std::regex re2("^7895432");
    std::cout << "re1:" << std::endl;
    FindMeText(re1, serial);
    std::cout << "re2:" << std::endl;
    FindMeText(re2, serial);
    std::cout << std::endl;
    std::string s;
    std::getline (std::cin,s);
}

Result:

Note that Python re.match searches for the pattern match at the start of string only, thus I suggest using ^ (start of string) at the beginning of each pattern.

相关阅读:
Top