9.Boost之正则regex
1正則表達(dá)式,案例1(如果使用的boost庫(kù)是64位的,要把VS設(shè)置成支持64位的,這樣的化,才可以運(yùn)行通過(guò))
#include <boost/regex.hpp>
#include <locale>
#include <iostream>
#include <string>
?
using namespace std;
?
void main()
{
??? //設(shè)置語(yǔ)言環(huán)境為:English
??? std::locale::global(std::locale("English"));
??? string? str = "chinaen8Glish";
??? boost::regex expr("\\w+\\d\\u\\w+");//d代表數(shù)字,
??? //匹配就是1,不匹配就是0
??? cout << boost::regex_match(str, expr) << endl;
?
??? cin.get();
??? //運(yùn)行結(jié)果為:1
}
2.通過(guò)正則取出匹配到的每部分
#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代表數(shù)字
??? boost::regex expr("(\\w+)\\d(\\w+)");
??? boost::smatch what;
??? //按照表達(dá)式檢索
??? if (boost::regex_search(str, what, expr))
??? {
??????? cout << what[0] << endl; //這里是字符串本身
??????? cout << what[1] << endl; //這里截取的字符串的第一部分
??????? cout << what[2] << endl; //這里截取的字符串的第二部分
??? }
??? else
??? {
??????? cout << "檢索失敗";
??? }
??? cin.get();
}
運(yùn)行結(jié)果:
3.通過(guò)正則表達(dá)式將正則中的數(shù)字編成指定的字符串。
#include <boost/regex.hpp>
#include <locale>
#include <iostream>
#include <string>
?
using namespace std;
?
void main()
{
??? string? str = "chinaen8? Glish9abv";
??? boost::regex expr("\\d");//d代表數(shù)字,
??? string? kongge = "______";
??? std::cout << boost::regex_replace(str, expr, kongge) << endl;
??? cin.get();
}
運(yùn)行結(jié)果:
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的9.Boost之正则regex的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 家常辣鱼的做法?
- 下一篇: 1Boost之TCP,Client an