views:

80

answers:

1

hello

#include <iostream>
#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;
}

i need to get the disallowed path / from Disallow: / what is wrong with my regex??

+5  A: 
string robotsfile = "User-Agent: *"
    "Disallow: /";

The string literals above are merged into "User-Agent: *Disallow: /" and there is no newline as you might have thought. Since your regular expression states that string must start with "Disallow" word, it does not match. The logically correct code would be something like this:

string robotsfile = "User-Agent: *\n"
    "Disallow: /";

or

string robotsfile = "User-Agent: *\nDisallow: /";
Vlad Lazarenko
if it is like `User-Agent: *\nDisallow: /posts/\nDisallow: /tags/\nDisallow: /unanswered/\n` i need to get /post/ only but in this regex i am getting /posts/Disallow: /tags/Disallow: /unanswered/ how to get the /post/ alone
llal
@llal: I am not that good at regular expressions so I'd recommend you post a separate question to get exact expression that will match what you want.
Vlad Lazarenko