当前位置:C++技术网 > 资讯 > Boost简单学习:12 字符串处理(三)正则表达式库 Boost.Regex

Boost简单学习:12 字符串处理(三)正则表达式库 Boost.Regex

更新时间:2015-12-06 16:40:59浏览次数:1+次

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

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

 #include <boost/regex.hpp>    
   #include <locale>    
   #include <iostream>    
   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;  
   }   

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

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

 #include <boost/regex.hpp>    
   #include <locale>    
   #include <iostream>    
   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;  
     }  
   }   


   函数 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 访问。

Boost.Regex 提供的第三个函数是 boost::regex_replace()。

#include <boost/regex.hpp>    
   #include <locale>    
   #include <iostream>    
   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;  
   }   


   除了待搜索的字符串和正则表达式之外,boost::regex_replace()函数还需要一个格式参数,它决定了子串、匹配正则表达式的分组如何被替换。如果正则表达式不包含任何分组,相关子串将被用给定的格式一个个地被替换。这样上面程序输出的结果为 _Boris_Sch?ling_。

   boost::regex_replace()函数总是在整个字符串中搜索正则表达式,所以这个程序实际上将三处空格都替换为下划线。

#include <boost/regex.hpp>    
   #include <locale>    
   #include <iostream>    
   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;  
   }   


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

   需要注意的是,对于正则表达式和格式有不同的标准。 这三个函数都可以接受一个额外的参数,用于选择具体的标准。 也可以指定是否以某一具体格式解释特殊字符或者替代匹配正则表达式的整个字符串。

#include <boost/regex.hpp>    
   #include <locale>    
   #include <iostream>    
   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, boost::regex_constants::format_literal) << std::endl;  

   }   

   此程序将boost::regex_constants::format_literal标志作为第四参数传递给函数 boost::regex_replace(),从而抑制了格式参数中对特殊字符的处理。 因为整个字符串匹配正则表达式,所以本例中经格式参数替换的到达的输出结果为 \2 \1。

   正如上一节末指出的那样,正则表达式可以和 Boost.StringAlgorithms 库结合使用。它通过 Boost.Regex 库提供函数如 boost::algorithm::find_regex() 、 boost::algorithm::replace_regex() 、 boost::algorithm::erase_regex() 以及 boost::algorithm::split_regex() 等等。由于 Boost.Regex 库很有可能成为即将到来的下一版 C++ 标准的一部分,脱离 Boost.StringAlgorithms 库,熟练地使用正则表达式是个明智的选择。