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