0

I would like to use a regex both as a pattern to search and a template to construct a string. (I'm using boost::regex because I'm on gcc 4.8.4 where apparently regex is not fully supported (until 4.9)):

That is, I want to construct a regex, pass it to a function, use the regex to match some files, then construct an output file name following the same pattern. For example:

Regex: "file_.*\.txt" to match things like "file_1.txt", "file_2.txt", etc. and then would like to construct from it Output: "file_all.txt"

That is, I want to match files starting with "file_" and ending with ".txt", then I want to fill in "all" between the "file_" and the ".txt", all from a single regex object.

We'll skip the matching to the regex as that is straightforward, but rather focus on the replacement:

#include <iostream>
#include <iterator>
#include <string>

#include <boost/regex.hpp>

std::string constructOutput(const boost::regex& myRegex)
{
    // How to replace the match to the center of the filenames here?
    // return boost::regex_replace(?, myRegex, "all");
}

int main()
{
   // We can do something like this, but it requires us to manually separate the "center" of the regex from the string, as well as keep around a string object and a regex object:
//   std::string myText = "File_.*.txt";
//   boost::regex myRegex("_.*\\.");

//   std::cout << '\n' << boost::regex_replace(myText, myRegex, "_all.") << '\n';

    // Want to do this:
    boost::regex myRegex("File_.*\\.txt");
    std::string outputString = constructOutput(myRegex);
    std::cout << outputString << std::endl;
}

Is something like this possible?

David Doria
  • 9,873
  • 17
  • 85
  • 147
  • 1
    If the filename always starts with "file_" and ends with ".txt" and the only part that changes is in between and that is what you want to replace with "all", why don't you hardcode the name to "file_all.txt" and then you don't need to do any replacing. I think you have omitted a crucial detail in your description of the problem. – Dark Falcon Mar 24 '16 at 12:12
  • @DarkFalcon Say then I pass constructOutput "other-.*thing.txt" and want it to construct "other-allthing.txt". So I guess in this case "all" will be hardcoded as the replacement, but I want it to replace the part of the regex that is the wildcard. – David Doria Mar 24 '16 at 12:15
  • But why? The point is if you know the prefix and suffix ahead of time, why are you not simply concatenating strings? – Dark Falcon Mar 24 '16 at 12:36
  • @DarkFalcon I guess the idea is to run a regex on the regex to replace the regex match pattern with a specific number. I guess the hang up is that you can't get the original string back from the regex object, so I was looking for a way around that. – David Doria Mar 24 '16 at 13:03
  • The original regex string is available via: `basic_string str() const;` on the `basic_regex` object. – Dark Falcon Mar 24 '16 at 13:10
  • @DarkFalcon Ah, I do see that now for boost::regex (I was using std::regex before, which doesn't have it). I guess that solves the problem for now, but if I ever move away from boost::regex how would you do the same thing with std::regex? – David Doria Mar 24 '16 at 13:20
  • There is no standardized way to do this. Let me ask a question though: How would you run a regex on that regex unless you can get the original string from it? You are going to need the string, not the regex object, if you wish to manipulate the regex itself. – Dark Falcon Mar 24 '16 at 13:55

0 Answers0