boost regex
来源:互联网 发布:淘宝优惠券网页 编辑:程序博客网 时间:2024/05/21 10:59
编译参数加上 -lboost_regex
#include <boost/xpressive/xpressive_dynamic.hpp>using namespace std;//c++ 验证ip地址bool VerifyIP() { boost::xpressive::cregex reg_ip = boost::xpressive::cregex::compile("(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])[." "](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])[.](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[" "1-9][0-9]|[0-9])[.](25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])"); /* 定义正则表达式 */ return boost::xpressive::regex_match("127.0.0.1", reg_ip);}#include <boost/regex.hpp>void test001(){ //( 1 ) (( 3 ) 2 )(( 5 )4)( 6 ) //(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)? //^协议://网址(x.x...x)/路径(n个/字串)/网页文件(xxx.xxx) const char *szReg = "(//w+)://((//w+//.)*//w+)((///w*)*)(///w+//.//w+)?"; const char *szStr = "http://www.cppprog.com/2009/0112/48.html"; { //字符串匹配 boost::regex reg(szReg); bool r = boost::regex_match(szStr, reg); // assert(r); cout<<"字符串匹配:"<<r<<endl; } { //提取子串 boost::cmatch mat; boost::regex reg(szReg); bool r = boost::regex_match(szStr, mat, reg); if (r) //如果匹配成功 { //显示所有子串 for (boost::cmatch::iterator itr = mat.begin(); itr != mat.end(); ++itr) { // 指向子串对应首位置 指向子串对应尾位置 子串内容 cout << itr->first - szStr << ' ' << itr->second - szStr << ' ' << *itr << endl; } } //也可直接取指定位置信息 if (mat[4].matched) cout << "Path is" << mat[4] << endl; } { //查找 boost::cmatch mat; boost::regex reg("//d+"); //查找字符串里的数字 if (boost::regex_search(szStr, mat, reg)) { cout << "searched:" << mat[0] << endl; } } { //替换 boost::regex reg(szReg); string s = boost::regex_replace(string(szStr), reg, "ftp://$2$5"); cout << "ftp site:" << s << endl; } { //替换2,把<>&转换成网页字符 string s1 = "(<)|(>)|(&)"; string s2 = "(?1<)(?2>)(?3&)"; boost::regex reg(s1); string s = boost::regex_replace(string("cout << a&b << endl;"), reg, s2, boost::match_default | boost::format_all); cout << "HTML:" << s << endl; } { //使用迭代器找出所有数字 boost::regex reg("//d+"); //查找字符串里的数字 boost::cregex_iterator itrBegin = make_regex_iterator(szStr, reg); //(szStr, szStr+strlen(szStr), reg); boost::cregex_iterator itrEnd; for (boost::cregex_iterator itr = itrBegin; itr != itrEnd; ++itr) { // 指向子串对应首位置 指向子串对应尾位置 子串内容 cout << (*itr)[0].first - szStr << ' ' << (*itr)[0].second - szStr << ' ' << *itr << endl; } } { //使用迭代器拆分字符串 boost::regex reg("/"); //按/符拆分字符串 boost::cregex_token_iterator itrBegin = make_regex_token_iterator(szStr, reg, -1); //使用-1参数时拆分,使用其它数字时表示取第几个子串,可使用数组取多个串 boost::cregex_token_iterator itrEnd; for (boost::cregex_token_iterator itr = itrBegin; itr != itrEnd; ++itr) { cout << *itr << endl; } } { //使用迭代器拆分字符串2 boost::regex reg("(.)/(.)"); //取/的前一字符和后一字符(这个字符串形象貌似有点邪恶-_-) int subs[] = {1, 2}; // 第一子串和第二子串 boost::cregex_token_iterator itrBegin = make_regex_token_iterator(szStr, reg, subs); //使用-1参数时拆分,使用其它数字时表示取第几个子串,可使用数组取多个串 boost::cregex_token_iterator itrEnd; for (boost::cregex_token_iterator itr = itrBegin; itr != itrEnd; ++itr) { cout << *itr << endl; } }}int main() { std::cout << VerifyIP() << std::endl; std::cout << "ok" << std::endl; test001(); return 0;}
0 0
- Boost.Regex
- Boost::Regex
- boost RegEx
- boost:regex
- boost::regex
- boost:regex
- BOOST::Regex
- boost regex
- boost -- regex
- boost::regex
- boost Regex
- <Boost> 正則表達式boost::regex
- boost::string or boost::regex
- 简介Boost.Regex
- boost.regex 浏览小记
- Boost.Regex 使用
- Boost::Regex Usage
- boost 库Regex使用
- iOS开发之AVAudioPlayer 音频播放
- SVN修改用户名与密码
- Linux中/usr与/var目录详解
- android下usb框架系列文章---(3)Storage框架整理
- 【转载】逻辑回归:使用SGD(Stochastic Gradient Descent)进行大规模机器学习
- boost regex
- 禁止任何按键touch事件对话框
- MySQL-Cluster测试过程中问题解决记录
- hadoop shuffle机制中针对中间数据的排序过程详解(源代码级)
- 多个Notification显示进度
- 验证码 注意一点存session时
- Java安全有效的实现两星期内自动登陆功能
- java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at org.hibernate.cfg.Configuration.<clinit>
- Linux USB子系统 (1)