文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>boost regex 数据有效性检测

boost regex 数据有效性检测

时间:2010-10-28  来源:tastesweet

头文件:  

#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
下载: 下载
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载