1

I am having trouble with a small regular expression which matches a single pipe character. I am always getting a regex_error exception with the error_escape code.

In it's most basic form, the escape on the pipe does not work in posix, extended posix or default syntax modes

std::regex("\\|", std::regex_constants::extended);

I have tried all 3 modes with 2 backslashes, just one and none.

I even tried to match using the escape for the actual value of the pipe character:

\\0x7c or \0x7c

With the same result.

What am i doing wrong? All The other regular expressions in my code work, including several similar literal character escapes (+, *, etc).

(Please do not suggest boost)

rscarson
  • 262
  • 3
  • 13
  • What leads you to believe it doesn't work? Or more accurately, what do you mean by "it doesn't work"? http://coliru.stacked-crooked.com/a/a0b6198080ecdcc5 – rici Jan 27 '16 at 23:33
  • Sorry about that, I somehow forgot to add that! I've updated the description with the exception I am receiving – rscarson Jan 28 '16 at 00:28
  • well, my attempt to reproduce the problem failed to do so (see link in previous comment). What compiler/standard library are you using? And which versions? – rici Jan 28 '16 at 00:51
  • g++ in c++11 mode... Version 4.8.1 – rscarson Jan 28 '16 at 00:59
  • minGW. I'm in a windows environment – rscarson Jan 28 '16 at 01:03
  • I don't believe that std::regex is fully supported by g++ v4.8.1, but it's a library issue and I have no idea how that would work out in a minGW environment. I'm pretty sure the answer is going to be "upgrade". I'll dig out minimum version numbers in a minute. – rici Jan 28 '16 at 01:05

1 Answers1

1

The earliest version of GNU libstdc++ which supports std::regex is v4.9.0, so if you are using libstdc++ v4.8, you are going to find lots of errors in the regex implementation. It's unfortunate that it was released with a partial and generally unusable implementation, but that's water under the bridge now.

(For the record, here's the implementation status from the v4.8.3 documentation. Although that document says "This page describes the C++ support in mainline GCC SVN, not in any particular release", it is, afaik, an actual record of what was available at that moment in time; however, the only hint is in the URL itself. The release notes for v4.9 list std::regex as being newly added.)

I'm sorry to say that I do not use MinGW, nor do I ever intend to, so I am not qualified to say whether there is a plausible workaround. Apparently, std::regex works fine in the Windows C++ standard library implementation (something else which I never use). And, of course, there is always boost but that seems to be out of scope for this question :)

rici
  • 234,347
  • 28
  • 237
  • 341
  • Shame, that's the latest version minGW has available. – rscarson Jan 28 '16 at 01:15
  • I'm working in a linux environment for now, but I will need windows platform compatibility, thus minGW – rscarson Jan 28 '16 at 01:27
  • @rscarson: According to the comments here -- http://stackoverflow.com/questions/33823969/regex-error-escape-while-parsing-escape-characters-using-mingw -- you can use a 64-bit mingw and thereby have v5.2 – rici Jan 28 '16 at 01:36