0

Let's assume I have this line in my etc/passwd:

xuser01:*:111000:201:User Name, School Info, Year:/homes/pc/xu/xuser01:/bin/ksh

I browse the file by lines.

From parameters I get usernames/userids that tells me which lines I should store into variable.

Using both regex_match and regex_search I got no results, while when I was testing it on online regex testers, it work like hell. Any idea why this is not working?

regExpr = "^(xuser01|xuser02)+:((.*):?)+";
if(regex_search(line, regex(regExpr)))
{ 
cout << "Boom I got you!" << endl;
}

line contains line read at the moment, it loops through the whole file, and doesn't find the string. I used regex_match too, same results.

Different regular expressions I tried: (xuser01|xuser02)+ and similar, designed to be almost 100% sure match (but still what I need to match), neither of it works in my C++ program, on online regex testers it does.

Advices?

Thanks in advance!

FanaticD
  • 1,416
  • 4
  • 20
  • 36
  • How do you read the line? Using C++ streams and `getline` (or something else), or using the old C `fgets` function? – Some programmer dude Apr 05 '15 at 09:59
  • @JoachimPilebord Using getline(), file is opened by ifstream.open(). – FanaticD Apr 05 '15 at 10:00
  • 1
    Which compiler are you using? Regex support is relatively recent (4.9) for gcc. – Wintermute Apr 05 '15 at 10:00
  • @wintermute I am using g++-4.9, I believe it would prompt an error during compilating if it was not supported, though. – FanaticD Apr 05 '15 at 10:02
  • 1
    I cannot reproduce the problem with gcc 4.9. Can you provide an [MCVE](http://stackoverflow.com/help/mcve)? Btw, the `+` in that regex don't make a lot of sense to me, since the `.*` matches greedily and it seems unlikely that you have a user `xuser01xuser02` or so. – Wintermute Apr 05 '15 at 10:03
  • 1
    @wintermute No, I do not have such a user, I will correct it. Thank you for your comment about inability to reproduce with 4.9. I did a bit of a digging, started from the scratch (MCVE), and just by the way checked server aliases. Guess what. g++-4.9 aliased to g++. I don't know why, but that's one of the evelest things I've ever seen. Problem solved I suppose. – FanaticD Apr 05 '15 at 10:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74507/discussion-between-fanaticd-and-wintermute). – FanaticD Apr 05 '15 at 10:19

1 Answers1

1

It looks like the quantifier + is preventing C++ from getting your matches. I think it is redundant in your regex since you only have a unique number of "xuser"s in your string.

This code works alright, gets to the cout line:

string line( "xuser01:*:111000:201:User Name, School Info, Year:/homes/pc/xu/xuser01:/bin/ksh" );
regex regExpr("^(xuser01|xuser02):((.*):?)");
if(regex_search(line, regExpr))
{ 
    cout << "Boom I got you!" << endl;
}

However, you did not indicate what you are looking for. Currently, it will only match 3 groups:

xuser01
*:111000:201:User Name, School Info, Year:/homes/pc/xu/xuser01:/bin/ksh
*:111000:201:User Name, School Info, Year:/homes/pc/xu/xuser01:/bin/ksh
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Does it really fail for you with the `+`s in there? They shouldn't make a difference here, and they don't for me. Not with gcc 4.9's libstdc++, nor with libc++ 3.5. – Wintermute Apr 05 '15 at 10:10
  • 1
    I am using C++ in VS2012. The code compiles fine, but I have an error on the `regex_search` line during the program execution. – Wiktor Stribiżew Apr 05 '15 at 11:06