bool validateCurrencyLine(string line){
cout << "TESTING LINE : " << line << endl;
string pattern = "[ ]*([A-Z]{3}) ([0-9]+)([ ]*|,[0-9]+[ ]*)";
boost::regex expr{pattern};
return boost::regex_match(line,expr);
}
int main()
{
string line;
while(getline(cin,line)){
cout << validateCurrencyLine(line) << endl;
}
return 0;
}
The content of test
file is as follows:
IDK 3453443
Now when I start a program using ./a.out < test
the result is
TESTING LINE : IDK 3453443
0
TESTING LINE :
0
My assumption is that the second line is printed because the testfile first line is actually
IDK 3453443 + enter
Am I correct?)
But the real problem is that when I start it like this: ./a.out
and input "IDK 3453443" and press enter. The result of this is:
TESTING LINE : IDK 3453443
1
Any thoughts why these two results differ?
Indeed the line ends are the culprit.
Look at the file in a hex editor, you will find 0d 0a
line ends (Windows or CRLF), where the code expects UNIX line-ends (just LF).
See it live:
You can work around it by 'eating' all whitespace at the end:
std::string pattern = "[ ]*([A-Z]{3}) ([0-9]+)(,[0-9]+)?\\s*";
Now both get accepted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.