From bb589042a94cf227a00756d7224949bff620acd9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 25 Jan 2024 14:53:12 +0100
Subject: [PATCH] dseries: fix handling of monthly dates for months 10-12

Ref. dynare#1918

(cherry picked from commit 7dd6b87662ca1087765c04477ef715d8c542c871)
---
 src/Statement.cc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/Statement.cc b/src/Statement.cc
index 42c9b1e4..e28d7f40 100644
--- a/src/Statement.cc
+++ b/src/Statement.cc
@@ -45,7 +45,11 @@ NativeStatement::writeOutput(ostream& output, [[maybe_unused]] const string& bas
                              [[maybe_unused]] bool minimal_workspace) const
 {
   using namespace boost::xpressive;
-  string date_regex = R"((-?\d+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[SsHh][1-2])))";
+  /* NB: in date_regex, for monthly dates, “1[0-2]” must come before “[1-9]” in the alternative,
+     otherwise 2023M12 will be matched as 2023M1 (see dynare#1918). Technically, it seems that
+     boost::xpressive does not look for the longest match in an alternation, but stops at the first
+     match from left to right. */
+  string date_regex = R"((-?\d+([YyAa]|[Mm](1[0-2]|[1-9])|[Qq][1-4]|[SsHh][1-2])))";
   sregex regex_lookbehind = sregex::compile(R"((?<!\$|\d|[a-zA-Z_]|-|'))" + date_regex);
   sregex regex_dollar = sregex::compile(R"((\$))" + date_regex);
 
-- 
GitLab