I'm trying to parse a URL with regex, and I've created the regex below:
std::regex r("https?://[0-9a-zA-Z.]+(:([0-9]+))?.*", std::regex_constants::extended);
(I'm trying to get the port out of the URL)
I can't seem to match any URL, but the regex works on several online regex testers for any URL. As a test, I created some much simpler code to check, which I can't get to match either.
#include <regex>
#include <string>
#include <iostream>
int main() {
std::string str = "http";
std::smatch match;
std::regex regex("https?");
if(regex_match(str, match, regex)) {
std::cout << "Match" << std::endl;
}else {
std::cout << "No Match" << std::endl;
}
return 0;
}
Compiling and running this yields "No Match", and I have no idea why. I originally leaned regexp's with ruby, so I may be misconceived, but this seems like it should match. Changing the regex to "http" makes it match, but I feel like "https?" should match "http" since the question mark means there can be zero or 1 of the character before it.
I'd really appreciate some help here. I've looked at a bunch of the documentation on c++ regular expressions and I can't seem to figure this one out. I have to be missing some small detail.
Any help would be great.
Thanks