2

I have the following code:

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main() {
    string s = "hello";
    regex e (".*");
    cout << boolalpha << regex_search(s,e) << endl;

    return 0;
}

Every time I compile/run it, it prints false. I have also tried to replace regex_search(s,e) with regex_search(s.c_str(),e). I have also changed regex e (".*"); to:

1) regex e ("");
2) regex e ("hello");
3) regex e ("^.*$");
4) regex e ("^.+$");
5) regex e ("^hello$");
6) regex e (".ell.");
7) regex e ("ell");

And all of these printed false as well. However, when I use regex_match() instead of regex_search(), expressions number 0 (original), 2, 6 return true, which means regex_match() works just fine;

My OS is Microsoft Windows [Version 10.0.16299.125]
My IDE is Code::Blocks 13.12
The compile/link commands are (I removed path info in this post):

-------------- Build: Debug in main (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g -std=c++11 -Wall  -c main.cpp -o main.o
mingw32-g++.exe -o main.exe main.o
Output file is main.exe with size 1.62 MB
Process terminated with status 0 (0 minute(s), 2 second(s))
0 error(s), 0 warning(s) (0 minute(s), 2 second(s))


-------------- Run: Debug in main (compiler: GNU GCC Compiler)---------------

Checking for existence: main.exe
Executing: "cb_console_runner.exe" "main.exe"
Process terminated with status 0 (0 minute(s), 1 second(s))

The question is simple: What am I missing/doing wrong?

JDB
  • 25,172
  • 5
  • 72
  • 123
MrRobot
  • 69
  • 5
  • Both functions evaluate to `true` for me. See [here](https://repl.it/repls/LargeFeminineArgali). – Arnav Borborah Feb 09 '18 at 20:11
  • I was able to reproduce this with gcc-4.8: https://wandbox.org/permlink/7BbVpktH3MPZ0Idc . It works on all the newer versions I tried, though – Justin Feb 09 '18 at 20:12
  • 2
    I know gcc had broken regex support for a while after it was introduced. (see this question: https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions) – Benjamin Lindley Feb 09 '18 at 20:14
  • 1
    Your answer is useful and I appreciate that you came back to update your question. Unfortunately, putting an answer in your question "hides" it... this question will appear to be unanswered as far as indexing and searching goes. The best solution would obviously be to add an answer to your question, but since it was closed as a duplicate, the next-best solution is to add an answer to the duplicate question. Your answer will be more visible to others looking for a solution, and may end up earning you more reputation (if you care about such things) – JDB Feb 09 '18 at 21:25

0 Answers0