9.Boost之正则regex
1正则表达式,案例1(如果使用的boost库是64位的,要把VS设置成支持64位的,这样的化,才可以运行通过)
#include <boost/regex.hpp>
#include <locale>
#include <iostream>
#include <string>
using namespace std;
void main()
{
//设置语言环境为:English
std::locale::global(std::locale("English"));
string str = "chinaen8Glish";
boost::regex expr("\\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>
using namespace std;
void main()
{
std::locale::global(std::locale("English"));
string str = "chinaen8Glish9abv";
//d代表数字
boost::regex expr("(\\w+)\\d(\\w+)");
boost::smatch what;
//按照表达式检索
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>
using namespace std;
void main()
{
string str = "chinaen8 Glish9abv";
boost::regex expr("\\d");//d代表数字,
string kongge = "______";
std::cout << boost::regex_replace(str, expr, kongge) << endl;
cin.get();
}
运行结果:
与50位技术专家面对面20年技术见证,附赠技术全景图
总结
以上是生活随笔为你收集整理的9.Boost之正则regex的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 家常辣鱼的做法?
- 下一篇: 1Boost之TCP,Client an