zoukankan      html  css  js  c++  java
  • greta一些简单实用的字符串匹配

    代码
    #if 0//整数
        match_results results;
        tstring str(
    "f34");
        rpattern pat(
    "^[+|-]?\\d+\\d*$");
        match_results::backref_type br 
    = pat.match(str, results);
        
    if (br.matched)
        {
            tcout 
    << "match" << std::endl;
        }
        
    else
        {
            tcout 
    << "not match" << std::endl;
        }
    #endif

    #if 0//正整数
        match_results results;
        tstring str(
    "+323f444");
        rpattern pat(
    "^[+]?\\d+\\d*$");
        match_results::backref_type br 
    = pat.match(str, results);
        
    if (br.matched)
        {
            tcout 
    << "match" << std::endl;
        }
        
    else
        {
            tcout 
    << "not match" << std::endl;
        }
    #endif

    #if 0//浮点数
        match_results results;
        tstring str(
    "-3.23444");
        rpattern pat(
    "^[+|-]?[0-9]+\\.?[0-9]+$");//^\-?[0-9]*\.?[0-9]*$
        match_results::backref_type br = pat.match(str, results);
        
    if (br.matched)
        {
            tcout 
    << "match" << std::endl;
        }
        
    else
        {
            tcout 
    << "not match" << std::endl;
        }
    #endif

    #if 0//字母
        match_results results;
        tstring str(
    ".asdf");
        rpattern pat(
    "^[a-z]*$");//^\-?[0-9]*\.?[0-9]*$
        match_results::backref_type br = pat.match(str, results);
        
    if (br.matched)
        {
            tcout 
    << "match" << std::endl;
        }
        
    else
        {
            tcout 
    << "not match" << std::endl;
        }
    #endif

     其他一些实用代码

    代码
    #if 1//subsitute 替换
        subst_results results;
        tstring str(
    "a43sdf");
        REGEX_FLAGS dw 
    = GLOBAL | NOCASE;
        rpattern pat(
    "[0-9]","d",dw);
        
    int cnts = pat.substitute(str,results);

        subst_results::backref_vector vec 
    = results.all_backrefs();
        subst_results::backref_vector::iterator iter;
        std::
    string m_strResult = "";
        
    for( iter = vec.begin(); iter != vec.end(); iter++ )
        {
            
    string _str = (*iter).str();
            m_strResult 
    += _str.c_str();
        }

    #endif

    #if 0//count 计算正则表达式在串中出现的次数
        tstring str(
    "asdf");
        rpattern pat(
    "[a-z]");
        
    int cnts = pat.count(str);

    #endif

    #if 0//split 用正则表达式作为分隔符来切分串
        split_results results;
        tstring str(
    "asdf");
        rpattern pat(
    "[a-z]");
        
    int cnts = pat.split(str, results);

    #endif
  • 相关阅读:
    8-6.布局元素实战
    Unity3D NGUI插件(3.12/2018/2019)
    GoLang 数据结构-二叉树
    GoLang 数据结构-哈希表(散列表)
    GoLang 四大经典排序(冒泡排序,选择排序,插入排序,快速排序)写法及执行效率
    GoLang 数据结构-单向链表,双向链表,单向环形链表
    GoLang 数据结构-环形队列
    GoLang 数据结构-稀疏数组
    GoLang 海量用户聊天系统(TCP-Socket网络编程+Redis数据库+协程)
    GoLang 使用协程和管道获取随机数
  • 原文地址:https://www.cnblogs.com/lancidie/p/1950222.html
Copyright © 2011-2022 走看看