deelx.h正则库使用

deelx.h正则库使用

官网首页

C++11已经支持正则表达式了basic_regex

我为什么选择用deelx.h库

  1. 轻量
  2. 无环境依赖
  3. 跨平台
  4. 可读性
  • C++使用 官方示例

    
    #include "deelx.h"
    
    int test_all_number(const char * string)
    {
        // declare, you can use 'static' if your regex does not change
        CRegexpT <char> regexp("\\d+");
    
        // test
        MatchResult result = regexp.MatchExact(string);
    
        // matched or not
        return result.IsMatched();
    }
    
    int main(int argc, char * argv[])
    {
        char * str1 = "12345";
        char * str2 = "12345 abcde";
    
        printf("'%s' => %s\n", str1, (test_all_number(str1) ? "yes" : "no"));
        printf("'%s' => %s\n", str2, (test_all_number(str2) ? "yes" : "no"));
    
        return 0;
    }
    
  • 以上示例可以看出

  • regexp("正则语法")

  • MatchExact("被匹配的字符串") 完全匹配匹配 ,只有完全匹配传入的字符串 result.IsMatched() 函数,返回才为true

  • Match("被匹配的字符串") 正则语法匹配 , 只要匹配到了, result.IsMatched() 函数返回为true

    以上两个方法在result.IsMatched()返回为true时都可以获取字符串

  • Compile 可以替换正则语法 ,多次匹配使用下面示例

     #include "deelx.h"
    
    int test_all_number(const char * string)
    {
        // declare, you can use 'static' if your regex does not change
        CRegexpT <char> regexp("\\d+");
    
        // test
        MatchResult result = regexp.MatchExact(string);
        
        //这里获取字符串
        if (result.IsMatched())
        {
            //匹配到的字符串起始
            int strat = result.GetStart();
            //匹配到的字符串结束 长度为 end-strat
            int end = result.GetEnd();
            //获取匹配到的字符串
            string getStr = std::string(string).substr(strat, end - strat);
        }
        
        //再次匹配 修改正则语法
        regexp.Compile("\\d+");
        //不完全匹配
        result = regexp.Match(string);
        if (result.IsMatched())
        {
            //匹配到的字符串起始
            int strat = result.GetStart();
            //匹配到的字符串结束 长度为 end-strat
            int end = result.GetEnd();
            //获取匹配到的字符串
            string getStr = std::string(string).substr(strat, end - strat);
        }
        
      // matched or not
        return result.IsMatched();
    }
    
    int main(int argc, char * argv[])
    {
        char * str1 = "12345";
        char * str2 = "12345 abcde";
    
        printf("'%s' => %s\n", str1, (test_all_number(str1) ? "yes" : "no"));
        printf("'%s' => %s\n", str2, (test_all_number(str2) ? "yes" : "no"));
    
        return 0;
    }
    
  • 正则语法

    1. 过滤ip

      ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
      
    2. 指定起始和结尾

      起始为'/' 中间为字母、数字、下划线、点 结尾为'?'
      (?<=/)[a-zA-Z0-9_.]+(?=\?)
      
      纯数字
      [0-9]
      (?<=/)[0-9]+(?=\?)
      
    3. 间隔指定位数字符串

      起始至结束0-7都是数字的8位字符串
      ^(.{7}[0-9])$
      
  • 我在vs2019和gcc 4.8.5 版本编译不过去,修改deelx.h 503行的源码 ,修改后如下

  • 编译版本高了,不支持这个写法

    
    template <class T> void CSortedBufferT <T> :: Add(const T & rT)
    {
    	if(m_bSortFreezed != 0)
    	{
    		CBufferT<T>::Append(rT);
    		return;
    	}
    
    	int a = 0, b = CBufferRefT<T>::m_nSize - 1, c = CBufferRefT<T>::m_nSize / 2;
    
    	while(a <= b)
    	{
    		int r = m_fncompare(&rT, &CBufferRefT<T>::m_pBuffer[c]);
    
    		if     ( r < 0 ) b = c - 1;
    		else if( r > 0 ) a = c + 1;
    		else break;
    
    		c = (a + b + 1) / 2;
    	}
    
    	CBufferT<T>::Insert(c, rT);
    }
    
posted @ 2021-03-20 12:31  做个奇怪的人  阅读(192)  评论(0编辑  收藏  举报