0

I've been trying to understand the behavior of the following simple regex command when using std::regex_replace. I'm trying to extract the filename on a linux system from a full path.

std::string fullPath = "/folder/subfolder/fname.xyz";
std::string fname = std::regex_replace( fullPath, std::regex( std::string(".*/")), std::string(""));

Where fname becomes an empty string. Is this expected behavior of std::regex_replace? I would have expected fname to be "fname.xyz" after the calls.

Update

I compile the program on Ubuntu 14.04 with compiler flags:

CXX = clang++ WFLAGS=-Wall -Wno-deprecated-declarations -Wno-unknown-pragmas CXXFLAGS = $(WFLAGS) -Werror -g -std=c++11 -pthread

The internal abi version of libstdc++ is 3.4.19. Extracted using:

readelf -sV /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | sed -n 's/.*@@GLIBCXX_//p' | sort -u -V | tail -1

The libstdc++ version of Ubuntu 14.04 (trusty) is 4.8.2.

tisch
  • 1,098
  • 3
  • 13
  • 30
  • What compiler/version are you using? [I'm getting your expected result](http://ideone.com/iRneCE), but some compilers have been slow at getting regex conformance. – Drew Dormann Apr 16 '15 at 15:56
  • clang++ (clang 3.5.0) on a x86_64 machine. – tisch Apr 16 '15 at 15:58
  • What version of libstdc++ or libc++ are you using? – Bill Lynch Apr 16 '15 at 16:19
  • Might be related to https://llvm.org/bugs/show_bug.cgi?id=21363 – Marco A. Apr 16 '15 at 16:20
  • Tested with more recent clang and had an expected result: http://rextester.com/live/AAX59324 – ixSci Apr 16 '15 at 16:21
  • `/sbin/ldconfig -p | grep stdc++ libstdc++.so.6 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libstdc++.so.6` – tisch Apr 16 '15 at 16:22
  • @tisch: Sadly, that's not helpful. Instead, could you tell me the compiler flags you are using and what release of the linux distribution you are using is? (or bsd or whatever) – Bill Lynch Apr 16 '15 at 16:23
  • Compiler flags: `CXX = clang++ WFLAGS=-Wall -Wno-deprecated-declarations -Wno-unknown-pragmas CXXFLAGS = $(WFLAGS) -Werror -g -std=c++11 -pthread` on ubuntu 14.04 – tisch Apr 16 '15 at 16:24
  • I can confirm the libstdc++ version is 3.4.19 using: `readelf -sV /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | sed -n 's/.*@@GLIBCXX_//p' | sort -u -V | tail -1` – tisch Apr 16 '15 at 16:29
  • @tisch: So that's actually an internal abi version. You're using [libstdc++-4.8.2](http://packages.ubuntu.com/trusty/libstdc++-4.8-dev). – Bill Lynch Apr 16 '15 at 16:31
  • @BillLynch thanks for the clarification on the version! – tisch Apr 16 '15 at 16:31
  • @nhahtdh: please clarify your decision to close this question. I don't see this as a duplicate of [link](http://stackoverflow.com/questions/12530406/is-gcc-4-7-and-gcc-4-8-buggy-about-regular-expressions). – tisch Apr 20 '15 at 10:13
  • @tisch: The question is not a duplicate, but the underlying issue is generally the same. There is currently no mechanism on SO to indicate that "all the problems in these question can be traced to this issue on upstream". I'm tired of seeing all these `` library questions all over again. – nhahtdh Apr 20 '15 at 10:24

1 Answers1

2

You are compiling with a libstdc++ before 4.9.0. This version of the c++ standard library does not properly implement <regex>.

The only real solution is to upgrade the version of libstdc++ that you are compiling against.

Would you like to know more?

Community
  • 1
  • 1
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173