9.Boost之正则regex

来源:互联网 发布:游戏鼠标 知乎 编辑:程序博客网 时间:2024/05/22 00:33


1正则表达式,案例1(如果使用的boost库是64位的,要把VS设置成支持64位的,这样的化,才可以运行通过)

#include<boost/regex.hpp>

#include<locale>

#include<iostream>

#include<string>

 

usingnamespacestd;

 

voidmain()

{

   //设置语言环境为:English

   std::locale::global(std::locale("English"));

   string str ="chinaen8Glish";

   boost::regexexpr("\\w+\\d\\u\\w+");//d代表数字,

   //匹配就是1,不匹配就是0

   cout <<boost::regex_match(str,expr) <<endl;

 

   cin.get();

   //运行结果为:1

}

2.通过正则取出匹配到的每部分

#include<boost/regex.hpp>

#include<locale>

#include<iostream>

#include<string>

 

usingnamespacestd;

 

voidmain()

{

   std::locale::global(std::locale("English"));

   stringstr ="chinaen8Glish9abv";

   //d代表数字

   boost::regexexpr("(\\w+)\\d(\\w+)");

   boost::smatchwhat;

   //按照表达式检索

   if (boost::regex_search(str,what,expr))

   {

       cout <<what[0] <<endl;//这里是字符串本身

       cout <<what[1] <<endl;//这里截取的字符串的第一部分

       cout <<what[2] <<endl;//这里截取的字符串的第二部分

   }

   else

   {

       cout <<"检索失败";

   }

   cin.get();

}

运行结果:

3.通过正则表达式将正则中的数字编成指定的字符串。

#include<boost/regex.hpp>

#include<locale>

#include<iostream>

#include<string>

 

usingnamespacestd;

 

voidmain()

{

   string str ="chinaen8 Glish9abv";

   boost::regexexpr("\\d");//d代表数字,

   string kongge ="______";

   std::cout << boost::regex_replace(str,expr,kongge) <<endl;

   cin.get();

}

运行结果:

0 0
原创粉丝点击