包括
#include <fstream>
#include <string>
#include<string>
#include<boost/algorithm/string.hpp>
#include<boost/regex.hpp>
#include <boost/algorithm/string/trim.hpp>
using namespace std;
using namespace boost;
int main() {
string robotsfile="User-Agent: *"
"Disallow: /";
regex exrp( "^Disallow:(.*)$");
match_results<string::const_iterator> what;
if( regex_search( robotsfile, what, exrp ) )
{
string s( what[1].first, what[1].second );
cout<< s;
}
return 0;
}
我需要从 Disallow:/
获取不允许的路径 /
我的正则表达式有什么问题??
最佳答案
string robotsfile = "User-Agent: *"
"Disallow: /";
上面的字符串文字被合并到“User-Agent: *Disallow:/”中,并且没有你想象的换行符。由于您的正则表达式声明字符串必须以“Disallow”字样开头,因此它不匹配。逻辑上正确的代码应该是这样的:
string robotsfile = "User-Agent: *\n"
"Disallow: /";
或
string robotsfile = "User-Agent: *\nDisallow: /";
关于c++ - 这个boost c++正则表达式代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3751387/