我有以下有效的 php 代码:
preg_match('/^(\d+):/', $string, $matches)
但是,当我尝试使用 Boost 的正则表达式库在 C++ 中执行此操作时,它不起作用。
regex expression1("^(\\d+):");
std::string filename = "C:\\Users\\root\\Desktop\\something.bin";
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
std::string contents((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
cmatch what;
if(regex_match(contents.c_str(), what, expression1))
{
cout<<"value is "<<what[1]<<endl;
}
我不知道为什么。 regex_match
适用于其他模式,但不适用于这个 ^(\\d+):
。
最佳答案
\
字符在 C 字符串中开始转义。试试 "^(\\d+):"
。
关于php preg_match 在 C++ 中与 Boost 的等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6293715/