I need to check whether a string consists of a special set of characters only (ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<
).
I could either use boost regex or a combination of isupper
and isdigit
. Which one would be considered the better choice when it comes to performance?
The string lenghts I am testing are around 100 characters.
bool IsValid(string& rString)
{
boost::regex regex("[0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ<]+");
boost::cmatch match;
return boost::regex_match(rString.c_str(), match, regex);
}
bool IsValid(string& rString)
{
for (string::size_type i = 0; i < rString.size(); ++i)
if (!isupper(rString[i]))
if (!isdigit(rString[i]))
if (rString[i] != '<')
return false;
return true;
}