0

First of all I would like to apologize for my english.

I'm working in a small C++ project and I would like to keep it small. Main problem is I need to use regex, but they don't work the way I need.

This works fine:

std::string s ("subject");
std::regex e ("(sub)(.*)");
if (std::regex_match (s,e))
  std::cout << "string object matched\n";

This one works fine too:

std::string s ("0");
std::regex e ("[0-9]", std::regex::extended);
if (std::regex_match (s,e))
  std::cout << "string object matched\n";

Even if I change std::regex::extended for std::regex::awk, basic or whatever this code doesn't work:

std::string s ("00");
std::regex e ("[0-9]{2}", std::regex::extended);
if (std::regex_match (s,e))
  std::cout << "string object matched\n";

Am I doing anything wrong?

I know std::regex aren't finished but I think they should work in such a simple regular expresion.

Anyway if anyone can point me a small regex library (not boost please) I will be thankfull.

ADITIONAL INFO

I'm working on Linux Ubuntu 12.10, compiler is gcc > 4.7

Edit:

@jalf: It compiles but it gives me a wrong result in the last one. It gives an error. Can't show any result cos I'm in a client today and I have no access to the program.

@KennyTM: Yes it's gcc/libstdc++ again, I have been reading this forums looking for an answer. Again, I can't use boost.

@Pete Becker: Yes, it's gcc > 4.7 version. Can't remember the exact version.

Thanatos
  • 1
  • 1
  • 2
  • 5
    Which implementation in which version are you using? These are all very new features, and I am sure the implementations have varying qualitites. – PlasmaHH Jun 26 '13 at 08:36
  • 4
    What do you mean it "doesn't work"? Does it compile? Does it give the wrong results? Which results did you expect, and which results did you actually get? – jalf Jun 26 '13 at 08:41
  • Is it gcc/libstdc++ again? There has been many questions about it already. Short answer: use Boost, or switch to clang/libc++, or don't use it. – kennytm Jun 26 '13 at 09:19
  • You're probably using gcc. Everyone involved should be embarrassed to have shipped the crap that's in their `regex` header. – Pete Becker Jun 26 '13 at 10:51

2 Answers2

1

It does work. On VS2012 I get a match for each.

  • "subject" matches "(sub)(.*)" because it contains "sub" and then .* (anything else).
  • "0" matches "[0-9]" because it contains a digit between 0 and 9 inclusive.
  • "00" matches "[0-9]{2}" because it contains two digits between 0 and 9 inclusive.
doctorlove
  • 18,872
  • 2
  • 46
  • 62
1

For me on VS2008 this seems to work:

#include <iostream>
#include <regex>

void main()
{
    std::string s ("00");
    std::tr1::regex e ("[0-9]{2}", std::tr1::regex::extended);
    if (std::tr1::regex_match (s,e))
        std::cout << "string object matched\n";
}  

At least if you are on windows then according to the summary of TR1 regular expressions on MSDN (http://msdn.microsoft.com/en-us/library/bb982727.aspx) it should work, only in some cases it seems that you may need to escape the curly braces (refer to the table in section "Grammar Summary"). If you are under Linux then I guess you need to consult the gcc documentation on this topic.

Asked a friend to run your original code on Linux (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)) and it did not work, so seems that the issue is Linux specific. Escaping curly braces also does not work.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71