views:

224

answers:

1

hello. I'm very new to c++ and boost. I'm trying to get the host name of a given url:

this is what I have now:

int main()
{
  string url = "http://www.amazon.com/gp/product/blabla";

 //Regular Expression from Javascript.
  boost::regex ex("/^((\w+):\/\/\/?)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#;\|]+)?([;\|])?([^\?#]+)?\??([^#]+)?#?(\w*)/");

  boost::regex_search(url, ex);
  // I want it to get "http://www.amazon.com/".
}

and I get tons of errors : "unrecognized character escape sequence" I took this regex from Javascript. I'm not sure I can do that.

What is the Regular Expressions types I can use in 'Boost.regex' except Perl? Is there any kind of regex converter? (because I'm converting tons of code from Javascript to c++, and I have few more Regular Expressions).

btw, this is the function I have in Javascript:

parseHostname: function(url)
 {
    m = /^((\w+):\/\/\/?)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#;\|]+)?([;\|])?([^\?#]+)?\??([^#]+)?#?(\w*)/.exec(url) || [];
    return m[6];
},

To see this example with PCRE and the complete code - link.

+1  A: 

Since the backslash (\) is an escape character in C (& C++) string constants, you need to escape it.

i.e replace all instances of \ with \\

LOL. I had the same problem with this post! All the backslashes disappeared because I forgot to escape them.

Check this page to see the different regex types available in Boost. It has a JavaScript type which just maps to normal, which implies the default is compatible with JavaScript regexes.

Ferruccio