0

i'm really new on C++ and i want to try a little bit C++. Normally i'm come from Java/PHP.

I have a String like;

std::string location = "file:///C:/Program Files (x86)/Demo/";

or

std::string location = "http://www.example.com/site.php";

How i can check:

  • a has location the domain www.example.com or example1.com
  • b starts the domain with http:// or https://

In Java or PHP i would take Regular Expression. But simply no idea how to start in C++.

My first things was to check http://:

std::string location = "";

if (strncmp(location.c_str(), "http://", 7)) {
    /* yepp */
} else {
    /* nope */
}

But that won't work.

I hope you can help me.

Adrian Preuss
  • 3,228
  • 1
  • 23
  • 43
  • 1
    Maybe try [http://en.cppreference.com/w/cpp/regex](http://en.cppreference.com/w/cpp/regex)? – user3553031 May 07 '14 at 03:47
  • 5
    By the way, strncmp doesn't return `bool`. It returns 0 on a match. – user3553031 May 07 '14 at 03:48
  • oh, thanks for the answers. `strncmp() == 0` is true, right? Thanks for the link. I try to play a little bit with that. – Adrian Preuss May 07 '14 at 03:50
  • 1
    Unfortunately, C++ doesn't come with "batteries included" as opposed to Java or PHP. Perhaps you can explore the regex functions linked above (only available in C++11 and not well supported yet), or use the standard string functions. For C++11 regex support, perhaps Boost can serve as a replacement. – Yong Jie Wong May 07 '14 at 03:50
  • @yjwong The batteries come in a separate package: http://www.boost.org – Cody Gray - on strike May 07 '14 at 05:31

1 Answers1

2

I'll attack your question in three ways, from most to least specific.

1 - Instead of reinventing the wheel, you can opt for suggestions already given here on Stack Overflow:

Easy way to parse a url in C++ cross platform?

2 - Regexps are indeed fully supported in C++. You might refer to the following as a start:

http://www.cplusplus.com/reference/regex/regex_search/

3 - In general, it is not advisable to utilize C-style functions such as strncmp to compare strings. The std::string class has several substring search functions that you'd best be using. The most basic of them is the following:

http://www.cplusplus.com/reference/string/string/find/

Hope this helps you get on the right track regardless of how you choose to proceed.

Community
  • 1
  • 1
thesentiment
  • 268
  • 1
  • 9
  • Not a problem. BTW, yjwong is correct in saying that the functions in may not be supported in your compiler. In that case, you may need to download the Boost libraries as yjwong suggested. For more information, go to http://www.boost.org/. It is probably a good idea to have the latest of these libraries on hand anyway even if you do have it built-in to your compiler. – thesentiment May 07 '14 at 04:06
  • Okay, it's to complex for a newby to download thirdparty libs. Want methods that generally I can use. Your first link is awesome! – Adrian Preuss May 07 '14 at 04:09