Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1609134
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: C/C++

2010-10-28 15:47:53

头文件:
 

#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
下载: 下载
阅读(1137) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~