• boost.regex正则表达式


    通配符

    一些简单的通配符:

    + :匹配一次或多次;
    * :匹配0次或多次;
    . 匹配除换行符以外的任意字符
    \w 匹配字母或数字或下划线或汉字 等价于 ‘[^A-Za-z0-9_]’。
    \s 匹配任意的空白符
    \d 匹配数字,需要转义\\d
    \b 匹配单词的开始或结束
    ^ 匹配字符串的开始
    $ 匹配字符串的结束

    转义字符

    cout << regex_match("123", regex("\d+")) << endl;     //结果为0,需要转义字符'\'
    cout << regex_match("123", regex("\\d+")) << endl;    //结果为1,完全匹配
    
    • 1
    • 2

    Boost C++ 的正则表达式库 Boost.Regex 可以应用正则表达式于 C++ 。 正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能。 虽然现在 C++ 仍然需要以 Boost C++ 库的形式提供这一功能,但是在将来正则表达式将进入 C++ 标准库。 Boost.Regex 库有望包括在下一版的 C++ 标准中。

    Boost.Regex 库中两个最重要的类是 boost::regex 和 boost::smatch, 它们都在 boost/regex.hpp 文件中定义。 前者用于定义一个正则表达式,而后者可以保存搜索结果。

    以下将要介绍 Boost.Regex 库中提供的三个搜索正则表达式的函数。

    regex_match函数

    函数 boost::regex_match() 用于字符串与正则表达式的比较。 在整个字符串匹配正则表达式时其返回值为 true 。

    #include  
    #include  
    #include  
    
    int main() 
    { 
      std::locale::global(std::locale("German")); 
      std::string s = "Boris Schäling"; 
      boost::regex expr("\\w+\\s\\w+"); 
      std::cout << boost::regex_match(s, expr) << std::endl; 
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    regex_search函数

    函数 boost::regex_search() 可用于在字符串中搜索正则表达式。

    #include  
    #include  
    #include  
    
    int main() 
    { 
      std::locale::global(std::locale("German")); 
      std::string s = "Boris Schäling"; 
      boost::regex expr("(\\w+)\\s(\\w+)"); 
      boost::smatch what; 
      if (boost::regex_search(s, what, expr)) 
      { 
        std::cout << what[0] << std::endl; 
        std::cout << what[1] << " " << what[2] << std::endl; 
      } 
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    函数 boost::regex_search() 可以接受一个类型为 boost::smatch 的引用的参数用于储存结果。 函数 boost::regex_search() 只用于分类的搜索, 本例实际上返回了两个结果, 它们是基于正则表达式的分组。

    存储结果的类 boost::smatch 事实上是持有类型为 boost::sub_match 的元素的容器, 可以通过与类 std::vector 相似的界面访问。 例如, 元素可以通过操作符 operator 访问。

    另一方面,类 boost::sub_match 将迭代器保存在对应于正则表达式分组的位置。 因为它继承自类 std::pair ,迭代器引用的子串可以使用 first 和 second 访问。如果像上面的例子那样,只把子串写入标准输出流, 那么通过重载操作符 << 就可以直接做到这一点,那么并不需要访问迭代器。

    请注意结果保存在迭代器中而 boost::sub_match 类并不复制它们, 这说明它们只是在被迭代器引用的相关字符串存在时才可以访问。

    另外,还需要注意容器 boost::smatch 的第一个元素存储的引用是指向匹配正则表达式的整个字符串的,匹配第一组的第一个子串由索引 1 访问。

    regex_replace函数

    Boost.Regex 提供的第三个函数是 boost::regex_replace()。boost::regex_replace() 函数还需要一个格式参数,它决定了子串、匹配正则表达式的分组如何被替换。如果正则表达式不包含任何分组,相关子串将被用给定的格式一个个地被替换。这样下面程序输出的结果为 Boris_Schäling 。boost::regex_replace() 函数总是在整个字符串中搜索正则表达式,所以这个程序实际上将三处空格都替换为下划线。

    #include  
    #include  
    #include  
    
    int main() 
    { 
      std::locale::global(std::locale("German")); 
      std::string s = " Boris Schäling "; 
      boost::regex expr("\\s"); 
      std::string fmt("_"); 
      std::cout << boost::regex_replace(s, expr, fmt) << std::endl; 
    } 
    
    
    #include  
    #include  
    #include  
    
    int main() 
    { 
      std::locale::global(std::locale("German")); 
      std::string s = "Boris Schäling"; 
      boost::regex expr("(\\w+)\\s(\\w+)"); 
      std::string fmt("\\2 \\1"); 
      std::cout << boost::regex_replace(s, expr, fmt) << std::endl; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    格式参数可以访问由正则表达式分组的子串,这个例子正是使用了这项技术,交换了姓、名的位置,于是结果显示为 Schäling Boris 。

    示例程序

    #include  
    #include  
    #include 
    #include 
    #include 
    
    
    boost::regex g_subexp("e[cl][oe][mc]");
    boost::regex g_expr("^select ([a-zA-Z]*)\\sfrom\\s([a-zA-Z]*)\\ssse");
    
    int Test()
    {
    	boost::cmatch what; 
        std::string content = "select name from table sse" ;
    	boost::cmatch sub;
    
    	if (boost::regex_match(content.c_str(), what, g_expr))
    	{
    		//regex_match :对整个输入块的匹配,整个块如不匹配则不能成功
           std::cout << "boost::cmatch size: " <<  what.size() << std::endl;
    		for(unsigned int i = 0; i < what.size(); i++)
    			std::cout << "str: " << what[i].str() << std::endl;
    	}
    	else
    	{
    		std::cout << "Error Match" << std::endl;
    	}
    	printf("%s\n",content.c_str());
    	while (boost::regex_search(content.c_str(), sub, g_subexp))
    	{
    		// 单字搜索,将每次匹配到的结果输出
    		printf("%s\n", sub.base());
    		printf("%s\n", sub[0].str().c_str());
    		content = sub[0].second;
            std::cout << "content: " << content << std::endl;
    	}
    	return 0 ;
    }
    
    
    int main(int argc, char *argv[]) 
    {  
        {
            std::string s = "Boris Schaling"; 
            boost::regex expr("\\w+\\s\\w+"); 
            std::cout << boost::regex_match(s, expr) << std::endl; 
        }
    
        {
            std::string s = "hello world"; 
            // 在匹配规则中,以括号()的方式来划分组别,实例中的规则共有两个括号,所以共有两组数据
            boost::regex expr("(\\w+)\\s(\\w+)"); 
            boost::smatch what; 
            if (boost::regex_search(s, what, expr)) 
            { 
                std::cout << what[0] << std::endl; 
                std::cout << what[1] << " " << what[2] << std::endl; 
            } 
        } 
    
        {
            std::string s = " Boris Schaling "; 
            boost::regex expr("\\s"); 
            std::string fmt("_"); 
            std::cout << boost::regex_replace(s, expr, fmt) << std::endl; 
        }
    
        Test();
    
        return 0;
    }
    /*
    output: 
    1
    hello world
    hello world
    _Boris_Schaling_
    boost::cmatch size: 3
    str: select name from table sse
    str: name
    str: table
    select name from table sse
    select name from table sse
    elec
    content: t name from table sse
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
  • 相关阅读:
    caxa复制尺寸错乱
    波卡生态中“中继链”、“DOT”的常见问题解答
    vue3 Vuex
    字符函数和字符串函数详解
    头歌php 表单语言进阶
    Docker容器内数据备份到系统本地
    Symfony Vue 教程
    linux系统常用命令
    Flody的应用
    教资认定报名照片要求小于190kb…
  • 原文地址:https://blog.csdn.net/Erice_s/article/details/133688902