mardi 24 juillet 2018

ISO Date Regex not working with std::regex [duplicate]

This question already has an answer here:

I’m having trouble figuring out why my regex is not working inside my c++ code. I’m using a rather complex regex to replace dates following ISO formatting inside a string (regex taken from here). The regex works perfectly fine in any regex checker I found online but inside my code it doesn’t replace the dates. I guess some part of the regex is not supported by std::regex or I’m missing some essential part needed by std::regex, since the code is working when I use a more trivial regex.

Example (VS):

// ExampleCode.cpp: 
//

#include "stdafx.h"
#include <string>
#include <regex>
#include <iostream>


int main()
{
    std::regex dateRegex("(?:[1-9]\d{3}(-?)(?:(?:0[1-9]|1[0-2])\1(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])\1(?:29|30)|(?:0[13578]|1[02])(?:\1)31|00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[0-5]))|(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)(?:(-?)02(?:\2)29|-?366))T(?:[01]\d|2[0-3])(:?)[0-5]\d(?:\3[0-5]\d)?(?:Z|[+-][01]\d(?:\3[0-5]\d)?)");
    std::string testString = "testtesttest 2018-07-19T09:02:30Z testtesttest";
    std::string result = std::regex_replace(testString, dateRegex, "Test");
    std::cout << testString;
    std::cout << std::endl;

    std::cout << result;
    std::cout << std::endl;

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire