pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


std::regex URL parser
Posted by Anonymous on Tue 23rd Jan 2024 13:52
raw | new post

  1. #include <regex>
  2. #include <iostream>
  3.  
  4. /* be aware of https://stackoverflow.com/questions/39645660/stdregex-to-match-begin-end-of-string */
  5. int main(int ac, char *av[]
  6. {
  7.   std::string Input{"myurl://foo:80/bar"};
  8.   std::regex Pattern{"^(.+?)://(((.+?)(:(.+?))?@)?((.+?)(?::([0-9]+)){0,1}))(/(.*?))?$",
  9.         std::regex_constants::ECMAScript};
  10.   std::smatch Matches;
  11.  
  12.   if (std::regex_search(Input, Matches,
  13.                         Pattern)) {
  14.     std::cout << Matches.size()
  15.               << " submatches found!";
  16.     for (size_t i{0}; i < Matches.size(); ++i) {
  17.       std::cout << "\n\nSubmatch " << i << ": "
  18.                 << Matches[i] << "\n  Length: "
  19.                 << Matches[i].length()
  20.                 << "\n  First Character: "
  21.                 << *Matches[i].first
  22.                 << "\n  Position: "
  23.                 << Matches.position(i) << std::endl;
  24.     }
  25.   }
  26.   return 0;
  27. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at