Advertisement
Lajamerr_Mittesdine

Link Matching

Jun 28th, 2018
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LinkParser::LinkParser(const QString &unparsedString)
  2. {
  3.     static QRegularExpression linkRegex = [] {
  4.         QFile tldFile(":/tlds.txt");
  5.         tldFile.open(QFile::ReadOnly);
  6.  
  7.         QTextStream t1(&tldFile);
  8.         t1.setCodec("UTF-8");
  9.  
  10.         // Read the TLDs in and replace the newlines with pipes
  11.         QString tldData = t1.readAll().replace(QRegExp("[\\n]"), "|").remove(QRegExp("[\\s]"));
  12.  
  13.         const QString urlRegExp =
  14.             "^"
  15.             // protocol identifier
  16.             "(?:(?:https?|ftps?)://)?"
  17.             // user:pass authentication
  18.             "(?:\\S+(?::\\S*)?@)?"
  19.             "(?:"
  20.             // IP address dotted notation octets
  21.             // excludes loopback network 0.0.0.0
  22.             // excludes reserved space >= 224.0.0.0
  23.             // excludes network & broacast addresses
  24.             // (first & last IP address of each class)
  25.             "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
  26.             "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"
  27.             "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"
  28.             "|"
  29.             // host name
  30.             "(?:(?:[_a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)"
  31.             // domain name
  32.             "(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*"
  33.             // TLD identifier
  34.             "(?:\\.(?:" +
  35.             tldData +
  36.             "))"
  37.             "\\.?"
  38.             ")"
  39.             // port number
  40.             "(?::\\d{2,5})?"
  41.             // resource path
  42.             "(?:[/?#]\\S*)?"
  43.             "$";
  44.  
  45.         return QRegularExpression(urlRegExp, QRegularExpression::CaseInsensitiveOption);
  46.     }();
  47.  
  48.     this->match_ = linkRegex.match(unparsedString);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement