0 c++ regex string-concatenation string-literals boost-regex
#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;
}
Run Code Online (Sandbox Code Playgroud)
我需要/从Disallow: /我的正则表达式错误中得到不允许的路径?
小智 5
string robotsfile = "User-Agent: *"
"Disallow: /";
Run Code Online (Sandbox Code Playgroud)
上面的字符串文字被合并到"User-Agent:*Disallow:/"中,并且没有您想到的换行符.由于您的正则表达式声明该字符串必须以"Disallow"字开头,因此它不匹配.逻辑上正确的代码是这样的:
string robotsfile = "User-Agent: *\n"
"Disallow: /";
Run Code Online (Sandbox Code Playgroud)
要么
string robotsfile = "User-Agent: *\nDisallow: /";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
318 次 |
| 最近记录: |