raagavan
raagavan

Reputation: 981

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

Upvotes: 2

Views: 391

Answers (2)

Martin Stone
Martin Stone

Reputation: 13007

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.

Upvotes: 2

rubber boots
rubber boots

Reputation: 15184

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

Upvotes: 4

Related Questions