2

I encountered a task which reuires a regex matching nothing.

C++ reference happily states that it already has such thing: http://en.cppreference.com/w/cpp/regex/basic_regex/basic_regex

1) Default constructor. Constructs an empty regular expression which will match nothing.

But in reality (clang 3.3) it happens not to be the case:

#include <string>
#include <regex>

int main(int argc, const char *argv[]) {

    std::regex re1;
    std::regex re2("");
    std::smatch rt1, rt2;

    bool r1 = std::regex_match(std::string(""), rt1, re1);
    bool r2 = std::regex_match(std::string(""), rt2, re2);
    std::cerr << "r1:" << r1 << ", r2:" << r2 << std::endl;
}

This program prints: r1:1, r2:1 What should mean that both regexes matched empty string.

Any idea what is wrong here and how to create "match nothing" regex ?

Dfr
  • 4,075
  • 10
  • 40
  • 63
  • By "nothing", do you mean `^$`? – moshbear Nov 30 '13 at 08:20
  • There's an empty string at the beginning of each string, that's what it's matching. – Barmar Nov 30 '13 at 08:20
  • @Barmar there's also the argument that an empty match pattern represents the universally accepting DFA `({q}, S, {q*S->q}, q, {q}})`, for some abstract charset `S` and (q*S->q meaning forall s in S, qxs->q) – moshbear Nov 30 '13 at 08:29
  • 1
    @Dfr: so you want a regex which never reports any match, right? As opposed to one which matches the absence of content? – John Zwinck Nov 30 '13 at 08:55
  • It would be more accurate to say that the default regex matches only an empty string. – Alan Stokes Nov 30 '13 at 09:46
  • @John, yes, also want to clearly understand what behaviour to expect from `re1` (regex constructed with empty constructor) and how it differs from `re2` (regex constructed with empty string) – Dfr Nov 30 '13 at 10:44
  • `clang` gives me `false`. Are you linking against GNU `libstdc++` by any chance? – Cubbi Nov 30 '13 at 15:42
  • @Cubbi, i guess so, i'm using clang in Fedora which itself depends on `libstdc++` – Dfr Dec 02 '13 at 06:35
  • 1
    @Dfr in that case, you should use boost.regex. The GNU libstdc++ regex library was just non-functional stubs until the current 4.9 – Cubbi Dec 02 '13 at 12:09
  • This clears things out, need to upgrade or use boost. – Dfr Dec 03 '13 at 06:48

1 Answers1

3

The default constructor for std::basic_regex constructs a regular expression that "does not match any character sequence". [re.regex.construct]/1. If your implementation matches an empty character sequence it's wrong.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165