简体   繁体   中英

Problem with special characters with RegEx in C++

I have an issue to replace a special characters in string (from IIS Sharepoint log files) that contains a domain name with forward slash and names that starts with t, n, r that makes confusions with regular expressions. my code is as follow:

std::setlocale(LC_ALL, ".ACP"); //Sets the locale to the ANSI code page obtained from the operating system. FR characters
std::string subject("2018-08-26 11:38:20 172.20.1.148 GET /BaseDocumentaire/Documents+de+la+page+Notes+de+services/Rappel+du+dispositif+de+Sécurité+relatif+aux+Moyens+de+paiement+et+d’épargne+en+agence.pdf - 80 0#.w|domainname\tonzaro 10.12.105.24 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64;+rv:61.0)+Gecko/20100101+Firefox/61.0 200 0 0 29984");
std::string result;
std::string g1, g2, g5, g9, g10; //str groups in regex

    try {
        std::regex re("(\\d{4}-\\d{2}-\\d{2})( \\d{2}:\\d{2}:\\d{2})( 172.20.1.148)( GET | POST | HEAD )((/.*){1,4}/.*.(pdf|aspx))( -.*)(domainname.[a-zA-Z0-9]*)( \\d+.\\d+.\\d+.\\d+)");
        std::sregex_iterator next(subject.begin(), subject.end(), re);
        std::sregex_iterator end;

        while (next != end) {
            std::smatch match = *next;
            std::cout << match.str() << "\n";
            std::cout << "-------------------------------------------" << "\n";
            g1 = match.str(1);
            g2 = match.str(2);
            g5 = match.str(5);
            g9 = match.str(9);
            g10 = match.str(10);
            next++;
        }

        std::cout << "Date: " + g1 << "\n";
        std::cout << "Time: " + g2 << "\n";
        std::replace(g5.begin(), g5.end(), '+', ' ');
        std::cout << "Link Document : " + g5 << "\n";
        std::cout << "User: " + g9 << "\n";
        std::cout << "IP: " + g10 << "\n";

    }
    catch (std::regex_error& e) {
        std::cout << "Syntax error in the regular expression" << "\n";
    }

My output for domain name is: domainname onzaro

Any help please for this problem with \\, \\t, \\n or \\r ?

I'd urge you to use raw string literals . This is solution designed for cases where the literal should not be processed in any way, such as yours.

The syntax is R "delimiter( raw_characters )delimiter" , so in your case it could be:

std::string subject(R"raw(2018-08-26 11:38:20 172.20.1.148 GET /BaseDocumentaire/Documents+de+la+page+Notes+de+services/Rappel+du+dispositif+de+Sécurité+relatif+aux+Moyens+de+paiement+et+d’épargne+en+agence.pdf - 80 0#.w|domainname\tonzaro 10.12.105.24 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64;+rv:61.0)+Gecko/20100101+Firefox/61.0 200 0 0 29984)raw");
std::regex re( R"raw((\d{4}-\d{2}-\d{2})( \d{2}:\d{2}:\d{2})( 172.20.1.148)( GET | POST | HEAD )((/.*){1,4}/.*.(pdf|aspx))( -.*)(domainname.[a-zA-Z0-9]*)( \d+.\d+.\d+.\d+))raw");

(I might have missed some superfluous \\ above). See it live.

Those special characters are called escape sequences are being processed in string literals at compilation level (in phase 5 to be precise). For raw string literals this transformation is suppressed.

You don't care about any special character handling. You just need to take care that ")delimiter" doesn't appear in your literal, which I imagine could happen in regex.

'\\t' is one character, a horizontal tab. If you want the characters \\ and t , you need to escape the backslash: "\\\\t" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM