开发者

whats wrong with my c++ regex match

开发者 https://www.devze.com 2023-02-12 00:38 出处:网络
i am writing an robots.txt parser in c++ boost::regex exrp( \"^User-agent:\\s*开发者_如何学运维(.*)\");

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

精彩评论

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