1

I am trying to match lines from on an input file using regex, and I have started with this test case to match for the types of lines that I want.

#include <iostream>
#include <regex>


int main(int argc, char *argv[]) {
    std::string s("%%%% Data and file %%%%");
    std::regex expr("%%%% (.*) %%%%", std::regex_constants::basic);
    bool match = regex_match(s, expr);
    std::cout<<match;
}

I expect the supplied regular expression to match the search string, as I have tested this expression on two separate regex testers. Why is C++ not matching this? Furthermore I have tried other expressions too that should match the search string, none of them work. I am beginning to think I am overlooking some subtlety in C++ regex implementation.

deepak
  • 2,045
  • 2
  • 21
  • 36
  • Yes it the compiler is g++, version 4.7.0. – deepak Apr 26 '13 at 03:20
  • 1
    Remove `std::regex_constants::basic`, most of the regex testers probably use ECMAScript grammar, also read [Is gcc4.7 buggy about regular expressions?](http://stackoverflow.com/questions/12530406/is-gcc4-7-buggy-about-regular-expressions) – Jesse Good Apr 26 '13 at 03:22
  • 10
    [gcc doesn't ship with a working implementation of ``](http://stackoverflow.com/questions/12530406/is-gcc4-7-buggy-about-regular-expressions/12665408#12665408); an alternative is to use Boost. – Praetorian Apr 26 '13 at 03:23
  • @JesseGood I tried without, still doesn't work. I added the `basic` constant because in an earlier expression I had [] brackets which didn't work with the ECMA implementation. – deepak Apr 26 '13 at 03:26
  • 2
    You can't use `regex` for gcc anyways, but it did give me [1 on ideone gcc 4.7.2](http://ideone.com/WZi2yV). – Jesse Good Apr 26 '13 at 03:27
  • @Praetorian Thanks I'll go through that link. I had not heard anything about that issue with gcc! – deepak Apr 26 '13 at 03:30
  • @JesseGood That is very interesting. Any thoughts on why the ideone result is coming positive? – deepak Apr 26 '13 at 03:32
  • 1
    No idea (perhaps somebody updated the code), use a [third party library](http://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines), re2 seems interesting. – Jesse Good Apr 26 '13 at 03:39
  • Jesse, thanks for introducing me to re2, it does look interesting. – deepak Apr 26 '13 at 04:34

0 Answers0