how *.\t can be compared using regular expression?
string str;
if (str = 1.\t) || (str = 2.\t) || ........ || (str = n.\t) {
// some code
}
Basically I 开发者_JAVA技巧have to check int followed by ".\t"
Regex you want is [0-9]+\.\t
Use Boost.Regex if you need a regex engine.
Here is an approach (without regular expressions), if your string is always an int followed by .\t
- Find the substring
.\t
in the string - If you find it, remove it from the string
- Try converting the string to a number (something like
boost::lexical_cast
as this will throw an exception if it cannot convert the string), failing that, trystrtol
and check that it consumed the full string.
afaik an additional lib has to be used in order to make use of regExp functionality in c++ and boost provides such. i found the explanations found under johndcook.com quite helpful.
A regular expression is overkill for this situation.
Try this:
bool testIntFollwedByTab(std::string const& test)
{
std::stringstrean teststream(test);
int num;
char c;
return (teststream >> std::noskipws >> num >> c) && (c == '\t');
}
- It puts the string
test
into a stream. - Then we read a number followed by a character from the stream. The manipulator
noskipws
indicates that white space is significant (otherwise leading white space before the number and the character are silently dropped).- If this fails (because there is not a number followed by a character) then the bad bit of the stream is set and the stream converts to false and the return statement returns false.
- If this is successful then we test to see if the character read was a '\t'
Or in normal code (with one line):
int num; char c;
if ((std::stringstream(str) >> std::noskipws >> num >> c) && (c == '\t'))
{
// Do Work
}
精彩评论