Changeset 19166


Ignore:
Timestamp:
08/14/18 22:25:32 (6 years ago)
Author:
tbretz
Message:
Better handling of the replacement for the filename.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/fits2sql.cc

    r19165 r19166  
    184184        "For example --const.mycolumn=42 would insert 42 into a column called mycolumn. "
    185185        "The column is created as INT UNSIGNED as default which can be altered by "
    186         "--sql-type. A special case is a value of the form `/regex/N/`. Here, the given "
    187         "regular expression is applied to the filename and N specifies the N-th "
    188         "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n"
     186        "--sql-type. A special case is a value of the form `/regex/format/`. Here, the given "
     187        "regular expression is applied to the filename and it is newly formated with "
     188        "the new format string. For details on how backreferences work, see for example "
     189        "the man-page of the sed utility.\n"
    189190        "\n"
    190191        "Usually the previously defined constant values are helpful to create an index "
     
    425426
    426427        boost::smatch match;
    427         if (boost::regex_match(val, match, boost::regex("\\/(.+)\\/([0-9]*)\\/")))
    428         {
    429             string s = match[1];
    430             size_t r = atoi(match[2].str().c_str());
    431 
    432             if (boost::regex_search(file, match, boost::regex(s)))
     428        if (boost::regex_match(val, match, boost::regex("\\/(.+)(?<!\\\\)\\/(.*)(?<!\\\\)\\/")))
     429        {
     430            const string reg = match[1];
     431            const string fmt = match[2];
     432
     433            val = boost::regex_replace(file, boost::regex(reg), fmt.empty()?"$0":fmt,
     434                                       boost::regex_constants::format_default|boost::regex_constants::format_no_copy);
     435
     436            if (verbose>0)
    433437            {
    434                 if (verbose>2)
    435                     for (size_t i=0; i<match.size(); i++)
    436                         cout << "Regex match " << setw(2) << i << ": `" << match[i] << "`" << endl;
    437 
    438                 val = match[r];
     438                cout << "Regular expression detected for constant column `" << *it << "`\n";
     439                cout << "Filename converted with /" << reg << "/ to /" << fmt << "/\n";
     440                cout << "Filename: " << file << '\n';
     441                cout << "Result: " << file << endl;
    439442            }
    440443        }
  • trunk/FACT++/src/root2sql.cc

    r19164 r19166  
    191191        "For example --const.mycolumn=42 would insert 42 into a column called mycolumn. "
    192192        "The column is created as INT UNSIGNED as default which can be altered by "
    193         "--sql-type. A special case is a value of the form `/regex/N/`. Here, the given "
    194         "regular expression is applied to the filename and N specifies the N-th "
    195         "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n"
     193        "--sql-type. A special case is a value of the form `/regex/format/`. Here, the given "
     194        "regular expression is applied to the filename and it is newly formated with "
     195        "the new format string. Uses the standard formatting rules to replace matches "
     196        "(those used by ECMAScript's replace method).\n"
    196197        "\n"
    197198        "Usually the previously defined constant values are helpful to create an index "
     
    332333int main(int argc, const char* argv[])
    333334{
     335    string input = "/this/is/my/dir/2018/12/13/12345_789_I.root";
     336    boost::regex rgx("([0-9]+)_([0-9]+)");
     337
     338    boost::smatch ma;
     339    cout << boost::regex_replace(input, rgx, "$1$2",  boost::regex_constants::format_default|boost::regex_constants::format_no_copy) << endl;
     340
     341    return 1;
     342
    334343    Time start;
    335344
     
    450459
    451460        boost::smatch match;
    452         if (boost::regex_match(val, match, boost::regex("\\/(.+)\\/([0-9]*)\\/")))
    453         {
    454             string s = match[1];
    455             size_t r = atoi(match[2].str().c_str());
    456 
    457             if (boost::regex_search(file, match, boost::regex(s)))
     461        if (boost::regex_match(val, match, boost::regex("\\/(.+)(?<!\\\\)\\/(.*)(?<!\\\\)\\/")))
     462        {
     463            const string reg = match[1];
     464            const string fmt = match[2];
     465
     466            val = boost::regex_replace(file, boost::regex(reg), fmt.empty()?"$0":fmt,
     467                                       boost::regex_constants::format_default|boost::regex_constants::format_no_copy);
     468
     469            if (verbose>0)
    458470            {
    459                 if (verbose>2)
    460                     for (size_t i=0; i<match.size(); i++)
    461                         cout << "Regex match " << setw(2) << i << ": `" << match[i] << "`" << endl;
    462 
    463                 val = match[r];
     471                cout << "Regular expression detected for constant column `" << *it << "`\n";
     472                cout << "Filename converted with /" << reg << "/ to /" << fmt << "/\n";
     473                cout << "Filename: " << file << '\n';
     474                cout << "Result: " << file << endl;
    464475            }
    465476        }
Note: See TracChangeset for help on using the changeset viewer.