2

I have the following regex:

(?<weekday>\w{3}),\s+(?<day>\d{1,2})\s+(?<month>\w{3})\s+(?<year>\d{4})\s+(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})\s+(?<timezone_offset>[+\-]\d{4})(?:\s+\((?<timezone_name>\w+)\))?

The goal is to match the following date: Fri, 2 Aug 2024 18:02:15 -0000 (UTC) The reason I am using regex is because it could be any of the following (basically anything in roughly that format):

Fri, 2 Aug 2024 18:02:15 -0000 (UTC)
Fri, 2 Aug 2024 18:02:15 +0000 (UTC)
Fri, 2 Aug 2024 18:02:15 0000 (UTC)
Fri, 2 Aug 2024   18:02:15   -0000 (UTC)
Fri, 2 Aug 2024 18:02:15 0000 (UTC)
Fri, 2   Aug   2024 18:02:15 -0000 (UTC)
Fri,                 2 Aug 2024 18:02:15 -0000
...

Using regex101.com I can match them all just fine under the PCRE setting. Unfortunately, with the following ctre code the first match contains the whole string:

static constexpr ctll::fixed_string ctll_pattern = { R"((?<weekday>\w{3}),\s+(?<day>\d{1,2})\s+(?<month>\w{3})\s+(?<year>\d{4})\s+(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})\s+(?<timezone_offset>[+\-]\d{4})(?:\s+\((?<timezone_name>\w+)\))?)" };
static constexpr ctll::fixed_string ctll_weekday = "weekday";
static constexpr ctll::fixed_string ctll_day = "day";
static constexpr ctll::fixed_string ctll_month = "month";
static constexpr ctll::fixed_string ctll_year = "year";
static constexpr ctll::fixed_string ctll_hours = "hours";
static constexpr ctll::fixed_string ctll_minutes = "minutes";
static constexpr ctll::fixed_string ctll_seconds = "seconds";
static constexpr ctll::fixed_string ctll_timezone_offset = "timezone_offset";
static constexpr ctll::fixed_string ctll_timezone_name = "timezone_name";

std::string s_weekday, s_month, s_timezone_name;
int day = 0, year = 0, hours = 0, minutes = 0, seconds = 0, timezone_offset = 0;

const auto all_matches = ctre::multiline_search_all<ctll_pattern>("Fri, 2 Aug 2024 18:02:15 -0000 (UTC)");
for (const auto& match : all_matches)
{
        std::cout << match.to_string() << "\n";
}

Is there some particular detail about CTRE which is stopping this from being matched? Unfortunately, I am rather new to regex so this is a quite hard to debug.

Bonus: any way to make this compile faster? Visual Studio 2022 MSVC 19.40 builds this is about 15 seconds in debug mode and ~30 min in release mode. This is unrelated though, but I figured someone may have some kind of knowledge here.

Compile Explorer

1 Answer 1

1

Each match will contain all capture groups. In your example, there will be only a single match. Capture groups can be extracted like this:

const auto all_matches = ctre::multiline_search_all<ctll_pattern>("Fri, 2 Aug 2024 18:02:15 -0000 (UTC)");
for (const auto& match : all_matches)
{
    std::cout << match.get<ctll_weekday>() << std::endl
        << match.get<ctll_day>() << std::endl
        << match.get<ctll_month>() << std::endl;
    // Or use numbered access.
    match.get<0>(); // capture 0 is the whole match
    match.get<1>(); // weekday
    match.get<2>(); // day
    // ...
}

See: https://github.com/hanickadot/compile-time-regular-expressions/blob/main/README.md#using-captures

3
  • Perfect, thank you! I did some matching before, but all the patterns had | between them which I believe makes them different matches whereas here they are all the same.
    – Eshy
    Commented Aug 3 at 8:09
  • Why do you use std::endl when just '\n' would suffice? Commented Aug 3 at 11:50
  • Several reasons: 1) I think std::endl is more explicit. 2) You want a std::endl at the end anyway to flush. 3) Mixing std::endl and '\n' is inconsistent. Furthermore, I think it would be very rare for a few extra flush calls to be a significant performance bottleneck. Commented Aug 3 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.