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!
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