头文件:
#ifndef __VALIDFILTER__H #define __VALIDFILTER__H
#define VALIDFILTER_FLOATNUM "([0-9]+).([0-9]+)|([0-9]+)" #define VALIDFILTER_FLOAT "([0-9]+).([0-9]+)" #define VALIDFILTER_NUM "[0-9]+" #define VALIDFILTER_ALPHABET "[a-z]+" #define VALIDFILTER_ ""
#include <iostream> #include <boost/regex.hpp> using namespace boost; using namespace std;
class validfilter { public: validfilter(void); ~validfilter(void);
bool IsFloatAndNum( std::string value); bool IsFloat( std::string value); bool IsNum( std::string value); bool IsAlphabet( std::string value);
private: bool validmatch( std::string& value,std::string regex_expression);
};
#endif
|
.cpp
#include "validfilter.h"
using namespace boost; using namespace std;
validfilter::validfilter(void) {
}
validfilter::~validfilter(void) {
}
bool validfilter::IsFloatAndNum( std::string value) { return validmatch( value,VALIDFILTER_FLOATNUM); }
bool validfilter::IsFloat( std::string value) { return validmatch( value,VALIDFILTER_FLOAT); }
bool validfilter::IsNum( std::string value) { return validmatch( value,VALIDFILTER_NUM); }
bool validfilter::IsAlphabet( std::string value) { return validmatch( value,VALIDFILTER_ALPHABET); }
bool validfilter::validmatch( std::string& value,std::string regex_expression) { if( value.empty() || regex_expression.empty() ) return false;
boost::regex expression(regex_expression);
if( regex_match( value,expression)) //字符串匹配
{ return true; } else { return false; }
}
|
demo测试程序
#include <iostream> #include <stdio.h> #include "validfilter.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { validfilter filter; bool state; std::string str;
while(1) { cout<<"input:"<<endl; cin>> str;
if( filter.IsFloatAndNum( str ) ) { cout<<"数字浮点"<<endl; continue; } if( filter.IsAlphabet( str ) ) { cout<<"字母"<<endl; continue; } if( filter.IsFloat( str )) { cout<<"浮点"<<endl; continue; } if( filter.IsNum(str) ) { cout<<"数字"<<endl; continue; } if( str == "quit") { return 0; } } return 0; }
|
|
文件: |
validfilter.rar |
大小: |
0KB |
下载: |
下载 | |
阅读(1171) | 评论(0) | 转发(0) |