0

I have created this regex and used the value to print out into a string.

 std::string s (filename);
  std::smatch m;
  std::regex e ("\/(?:.(?!\/))+$");   

  while (std::regex_search (s,m,e)) {
    for (auto p:m) std::cout << p << " ";

    s = m.suffix().str();   
  }
    std::string currFileName = s;

    std::string Original = filename;
    std::string newFile = Original + currFileName;
    std::cout << newFile;

The problem is it does not show in the terminal.

Printed value

/simple-loops.cc /home/fypj/build/simple-loops.cc

Expected Value

/home/fypj/build/simple-loops.cc/simple-loops.cc

You may ask what filename is

llvm::StringRef filename;
SourceLocation ST = f->getSourceRange().getBegin();
filename = Rewrite.getSourceMgr().getFilename(ST);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
HiWorld
  • 31
  • 7

1 Answers1

1

NOTE: Perhaps, the best way to proceed is by using path manipulation method described, say, in Getting a directory name from a filename post.

Answering your question...

You are trying to match the last part of a path and append it to the end. You are using a regex that does not match your string because you want to match one or more chars after the last / that are not / - but your input ends with /. "\/(?:.(?!\/))+$" also contains \/ wrong escape sequences, you should not escape the / symbols. Mind that this tempered greedy token like construct is not efficient when you need to "negate" just one symbol - you need negated character class.

So, if you want to do it with a regex, use a pattern that accounts for /s and use it with regex_replace:

#include <iostream>
#include <regex>
using namespace std;

int main() {
    std::regex reg("/?([^/]+)/?$");
    std::string s("/home/fypj/build/simple-loops.cc/");
    std::cout << std::regex_replace(s, reg, "/$1/$1/") << std::endl;
    return 0;
}

See the C++ demo

Pattern details:

  • /? - an optional /
  • ([^/]+) - Group 1 (can be referenced with $1 from the replacement patter) capturing one or more symbols other than / (the [^...] is a negated character class)
  • $ - end of string
Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Right now, I have been using (http://www.regexr.com) to get an expression but it seems that this website is not good – HiWorld Aug 04 '16 at 05:39
  • 1
    `std::regex` ECMAScript implementation is different from JS, so regexr is not so good to test C++ std::regex patterns. However, simple ones will do. Note that my demo above [produces `/home/fypj/build/simple-loops.cc/simple-loops.cc/`](https://ideone.com/Cs5NLY) result given your input is [`"/home/fypj/build/simple-loops.cc/"`](http://stackoverflow.com/questions/38736133/regex-ed-value-to-string/38736663?noredirect=1#comment64847593_38736133). Please be more specific about the input. – Wiktor Stribiżew Aug 04 '16 at 06:24
  • @HiWorld: If you failed to use this approach because of [this issue](http://stackoverflow.com/questions/38762492/regex-unable-to-print/38762657#38762657) please consider reviewing my suggestion again. – Wiktor Stribiżew Aug 04 '16 at 08:51
  • @wiktorsrtribizew Just testing out different options – HiWorld Aug 04 '16 at 08:58