开发者

whats wrong with my c++ regex match

i am writing an robots.txt parser in c++

 boost::regex exrp( "^User-agent:\s*开发者_如何学运维(.*)");

                 boost:: match_results<string::const_iterator> what;

                  if(boost::regex_search( robots, what, exrp ) )

                  {

                      string s( what[1].first, what[1].second );


                      cout<< s;
                  }

this should match the useragent with name * but it it returns all datas


You need the double backslash '\\' if you don't use c++0x raw strings.


If you want it to match only User-agent: * and not also (e.g.) User-agent: webcrawler you need

"^User-agent:\\s*\\*"

The * character has a special meaning, so must be escaped with \. The (.*) in your code matches zero or more occurrences of any character and captures the match.

Edit: You also need to escape the backslashes as pointed out by rubber boots.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜