Index: /trunk/FACT++/src/fits2sql.cc
===================================================================
--- /trunk/FACT++/src/fits2sql.cc	(revision 19165)
+++ /trunk/FACT++/src/fits2sql.cc	(revision 19166)
@@ -184,7 +184,8 @@
         "For example --const.mycolumn=42 would insert 42 into a column called mycolumn. "
         "The column is created as INT UNSIGNED as default which can be altered by "
-        "--sql-type. A special case is a value of the form `/regex/N/`. Here, the given "
-        "regular expression is applied to the filename and N specifies the N-th "
-        "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n"
+        "--sql-type. A special case is a value of the form `/regex/format/`. Here, the given "
+        "regular expression is applied to the filename and it is newly formated with "
+        "the new format string. For details on how backreferences work, see for example "
+        "the man-page of the sed utility.\n"
         "\n"
         "Usually the previously defined constant values are helpful to create an index "
@@ -425,16 +426,18 @@
 
         boost::smatch match;
-        if (boost::regex_match(val, match, boost::regex("\\/(.+)\\/([0-9]*)\\/")))
-        {
-            string s = match[1];
-            size_t r = atoi(match[2].str().c_str());
-
-            if (boost::regex_search(file, match, boost::regex(s)))
+        if (boost::regex_match(val, match, boost::regex("\\/(.+)(?<!\\\\)\\/(.*)(?<!\\\\)\\/")))
+        {
+            const string reg = match[1];
+            const string fmt = match[2];
+
+            val = boost::regex_replace(file, boost::regex(reg), fmt.empty()?"$0":fmt,
+                                       boost::regex_constants::format_default|boost::regex_constants::format_no_copy);
+
+            if (verbose>0)
             {
-                if (verbose>2)
-                    for (size_t i=0; i<match.size(); i++)
-                        cout << "Regex match " << setw(2) << i << ": `" << match[i] << "`" << endl;
-
-                val = match[r];
+                cout << "Regular expression detected for constant column `" << *it << "`\n";
+                cout << "Filename converted with /" << reg << "/ to /" << fmt << "/\n";
+                cout << "Filename: " << file << '\n';
+                cout << "Result: " << file << endl;
             }
         }
Index: /trunk/FACT++/src/root2sql.cc
===================================================================
--- /trunk/FACT++/src/root2sql.cc	(revision 19165)
+++ /trunk/FACT++/src/root2sql.cc	(revision 19166)
@@ -191,7 +191,8 @@
         "For example --const.mycolumn=42 would insert 42 into a column called mycolumn. "
         "The column is created as INT UNSIGNED as default which can be altered by "
-        "--sql-type. A special case is a value of the form `/regex/N/`. Here, the given "
-        "regular expression is applied to the filename and N specifies the N-th "
-        "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n"
+        "--sql-type. A special case is a value of the form `/regex/format/`. Here, the given "
+        "regular expression is applied to the filename and it is newly formated with "
+        "the new format string. Uses the standard formatting rules to replace matches "
+        "(those used by ECMAScript's replace method).\n"
         "\n"
         "Usually the previously defined constant values are helpful to create an index "
@@ -332,4 +333,12 @@
 int main(int argc, const char* argv[])
 {
+    string input = "/this/is/my/dir/2018/12/13/12345_789_I.root";
+    boost::regex rgx("([0-9]+)_([0-9]+)");
+
+    boost::smatch ma;
+    cout << boost::regex_replace(input, rgx, "$1$2",  boost::regex_constants::format_default|boost::regex_constants::format_no_copy) << endl;
+
+    return 1;
+
     Time start;
 
@@ -450,16 +459,18 @@
 
         boost::smatch match;
-        if (boost::regex_match(val, match, boost::regex("\\/(.+)\\/([0-9]*)\\/")))
-        {
-            string s = match[1];
-            size_t r = atoi(match[2].str().c_str());
-
-            if (boost::regex_search(file, match, boost::regex(s)))
+        if (boost::regex_match(val, match, boost::regex("\\/(.+)(?<!\\\\)\\/(.*)(?<!\\\\)\\/")))
+        {
+            const string reg = match[1];
+            const string fmt = match[2];
+
+            val = boost::regex_replace(file, boost::regex(reg), fmt.empty()?"$0":fmt,
+                                       boost::regex_constants::format_default|boost::regex_constants::format_no_copy);
+
+            if (verbose>0)
             {
-                if (verbose>2)
-                    for (size_t i=0; i<match.size(); i++)
-                        cout << "Regex match " << setw(2) << i << ": `" << match[i] << "`" << endl;
-
-                val = match[r];
+                cout << "Regular expression detected for constant column `" << *it << "`\n";
+                cout << "Filename converted with /" << reg << "/ to /" << fmt << "/\n";
+                cout << "Filename: " << file << '\n';
+                cout << "Result: " << file << endl;
             }
         }
