CPP + regex for URL validation

I want to create a regex in C ++ {MFC} that validates a url.

A regular expression must satisfy the following conditions.

Valid URL: - http://cu-241.dell-tech.co.in/MyWebSite/ISAPIWEBSITE/Denypage.aspx/ http://www.google.com http://www.google.co.in

Invalid URL: -

How can we develop a common regular expression that satisfies the above condition. Please help us by providing a regex that will handle the above script in CPP {MFC}

+4
c ++ c regex visual-c ++ mfc
Apr 11 '11 at 10:50
source share
2 answers

Have you tried using the RFC 3986 proposal? If you can use GCC-4.9, you can go directly to <regex> .

It states that using ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? you can get as sub-matrices:

  scheme = $2 authority = $4 path = $5 query = $7 fragment = $9 

For example:

 int main(int argc, char *argv[]) { std::string url (argv[1]); unsigned counter = 0; std::regex url_regex ( R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)", std::regex::extended ); std::smatch url_match_result; std::cout << "Checking: " << url << std::endl; if (std::regex_match(url, url_match_result, url_regex)) { for (const auto& res : url_match_result) { std::cout << counter++ << ": " << res << std::endl; } } else { std::cerr << "Malformed url." << std::endl; } return EXIT_SUCCESS; } 

Then:

 ./url-matcher http://localhost.com/path\?hue\=br\#cool Checking: http://localhost.com/path?hue=br#cool 0: http://localhost.com/path?hue=br#cool 1: http: 2: http 3: //localhost.com 4: localhost.com 5: /path 6: ?hue=br 7: hue=br 8: #cool 9: cool 
+9
Jul 24. '15 at 14:33
source share

look at http://gskinner.com/RegExr/ , there is a community tab on the right, where you will find the regular expressions entered into it. There is a URI category, not sure if you will find exactly what you need, but this is a good start.

0
Apr 11 2018-11-11T00:
source share



All Articles