#include <iostream> #include <boost/regex.hpp> #include <string> using namespace std; using namespace boost; /*搜索字符串中是否含有子字符串 int main( int argc, char* argv[] ) { char *buf = "This is boost::regex example boost::regex"; boost::regex exampleregex( "boost::regex" ); boost::cmatch result; if( boost::regex_search( buf, result, exampleregex ) ) { std::cout << result.str() << std::endl; } //boost::regex_replace(buf,) return 0; }*/ //写了个去除左侧无效字符(空格,回车,TAB)的正则表达式。 /*void main() { std::string testString = " \r\n Hello World ! GoodBye World\r\n"; std::string TrimLeft = "([\\s\\r\\n\\t]*)(\\w*.*)"; boost::regex expression(TrimLeft); testString = boost::regex_replace( testString, expression, "$2" ); std::cout<< "TrimLeft:" << testString <<std::endl; }*/ //关于重复的贪婪 /*void main() { std::string regstr = "(.*?)(age)(.*?)(\\d{2})"; boost::regex expression(regstr); std::string testString = "My age is 28 His age is 27"; boost::smatch what; std::string::const_iterator start = testString.begin(); std::string::const_iterator end = testString.end(); while( boost::regex_search(start, end, what, expression) ) { std::string name(what[1].first, what[1].second); std::string age(what[4].first, what[4].second); std::cout<< "Name:" << name.c_str() << std::endl; std::cout<< "Age:" <<age.c_str() << std::endl; start = what[0].second; } }*/ //regex_search学习 //regex_search与regex_match基本相同,只不过regex_search不要求全部匹配,即部份匹配(查找)即可。 //检测字符串里是否含有数字 //简单例子: /*void main() { std::string regstr = "(\\d+)"; boost::regex expression(regstr); std::string testString = "192.168.4.1"; boost::smatch what; if( boost::regex_search(testString, expression) ) { std::cout<< "Have digit" << std::endl; } } //用于打印出所有的数字 void main() { std::string regstr = "(\\d+)"; regstr = "(?<=(<a href="\" mce_href="\"")(.+?)(\">)(.+?)(?=</a>))"; //regstr = "((?=<a href="\" mce_href="\"").+(?=\">.+</a>))"; boost::regex expression(regstr); std::string testString = "sd<a href="\" mce_href="\""http:/www.baidu.com/\" target='_blank'>aacc</a>fs<a href="\" mce_href="\""http:/www.baidu.com/\">aacc</a>df192dfsd<a href="\" mce_href="\""http:/www.gjw123.com/\">bbdd</a>.168sdfs.sdfs4dsf.sfdsd1asfscvasdf"; boost::smatch what; std::string::const_iterator start = testString.begin(); std::string::const_iterator end = testString.end(); while( boost::regex_search(start, end, what, expression) ) { std::cout<< "Have digit:" ; std::string msg(what[0].first, what[0].second); std::cout<< msg.c_str() << std::endl; start = what[0].second; } } */ //#include "stdafx.h" #include <cstdlib> #include <stdlib.h> #include <boost/regex.hpp> #include <string> #include <iostream> using namespace std; //using namespace boost; boost::wregex expression(L"^\\s*我+\\s*[想|爱|恨|扁]+\\s*你"); int main(int argc, char* argv[]) { locale loc( "Chinese-simplified" ); wcout.imbue(loc); std::wstring in = L"我我我我 爱爱爱爱爱 你"; static boost::wsmatch what; cout << "enter test string" << endl; //getline(cin,in); if(boost::regex_match(in.c_str(), what, expression)) { for(int i=0;i<what.size();i++) wcout<<L"str :"<<what[i].str()<<endl; } else { wcout<<L"Error Input"<<endl; } return 0; } /* #include <iostream> #include <string> #include <boost/regex.hpp> int main() { std::string str = "I'm singing while you're dancing."; std::string pattern = "(\\b\\w+(?=ing\\b))"; boost::regex ee_all(pattern); boost::smatch result; std::string::const_iterator it = str.begin(); std::string::const_iterator end = str.end(); while(boost::regex_search(it, end, result, ee_all)) { std::cout << result[0] << '\n'; it = result[0].second; } } */ /*void main() { std::string testString = "sdfs<a href="\" mce_href="\""http:/www.baidu.com/\">aacc</a>df192dfsd<a href="\" mce_href="\""http:/www.gjw123.com/\">bbdd</a>.168sdfs.sdfs4dsf.sfdsd1asfscvasdf"; cmatch what; regex expression( "(?=<a\\s+href=\"(.+?)\">(.+?)</a>)"); if(regex_match(testString,what,expression)) { int sun = what.size(); cout<<sun<<endl; for(int i=0;i <what.size();i++); cout << "str: " <<what.str() <<endl; } else { cout << "Error "<<endl; } }*/ /* 四:regex_match例子代码学习 1 我们经常会看一个字符串是不是合法的IP地址,合法的IP地址需要符合以下这个特征: xxx.xxx.xxx.xxx 其中xxx是不超过255的整数 正则表达式找到上面的这种形式的字符串相当容易,只是判断xxx是否超过255就比较困难了(因为正则表达式是处理的文本,而非数字) OK,我们先来处理一个数字,即:xxx。找到一种表达式来处理这个数字,并且保证这个数字不会超过255 第一种情况:x,即只有一个数字,它可以是0~9 ,用\d 表示 第二种情况:xx,即有两个数字,它可以是00~99,用\d\d 表示 第三种情况:xxx,这种情况分为两种,一种是 1xx,可以用 1\d\d 表示 另外一种是 2xx,这又分为两种 2[1234]\d 和 25[12345] 好了组合起来 1?\d{1,2}|2[1234]\d|25[12345] 既可以标识一个不大于255的数字字符串 嗯,我们现在需要重复这种情况既可: (1?\d{1,2}|2[1234]\d|25[12345])\.(1?\d{1,2}|2[1234]\d|25[12345])\.(1?\d{1,2}|2[1234]\d|25[12345])\.(1?\d{1,2}|2[1234]\d|25[12345]) 呵呵,长是长了点,我试图用boost支持的子表达式缩短,但是没有达到效果,请各位了解boost的正则表达式的达人指点: (1?\d{1,2}|2[1234]\d|25[12345])\.\1$\.\1$\.\1$ (参看反向索引:http://www.boost.org/libs/regex/doc/syntax_perl.html 似乎反向只能匹配与第一个字符完全一样的字符串,与我们的需求不同) Example: void main() { std::string regstr = "(1?\\d{1,2}|2[1234]\\d|25[12345])\\.(1?\\d{1,2}|2[1234]\\d|25[12345])\\.(1?\\d{1,2}|2[1234]\\d|25[12345])\\.(1?\\d{1,2}|2[1234]\\d|25[12345])"; boost::regex expression(regstr); std::string testString = "192.168.4.1"; std::cout << "enter test string" << std::endl; std::getline(std::cin,testString); if( boost::regex_match(testString, expression) ) { std::cout<< "This is ip address" << std::endl; } else { std::cout<< "This is not ip address" << std::endl; } std::getline(std::cin,testString); } */ //这个例子会把所有的IP的单个数字答应出来 // /*void main() { std::string regstr = "(1?\\d{1,2}|2[1234]\\d|25[12345])\\.(1?\\d{1,2}|2[1234]\\d|25[12345])\\.(1?\\d{1,2}|2[1234]\\d|25[12345])\\.(1?\\d{1,2}|2[1234]\\d|25[12345])"; boost::regex expression(regstr); std::string testString = "192.168.4.1"; boost::smatch what; if( boost::regex_match(testString, what, expression) ) { std::cout<< "This is ip address" << std::endl; for(int i = 1;i <= 4;i++) { std::string msg(what[i].first, what[i].second); std::cout<< i << ":" << msg.c_str() << std::endl; } } else { std::cout<< "This is not ip address" << std::endl; } }*/ /* void main() { std::string regstr = "a+"; boost::regex expression(regstr); std::string testString = "aaa"; // 匹配至少一个a if( boost::regex_match(testString, expression) ) { std::cout<< "Match" << std::endl; } else { std::cout<< "Not Match" << std::endl; } }*/ /* 找出非奇数字符 元字符用来表示取反。regex reg("[^13579]");它表示一个取反的字符类,可以匹配任意非奇数的字符。 void main() { regex reg("[^13579]"); string s="012abcd3456789"; sregex_iterator it(s.begin(),s.end(),reg); sregex_iterator end; while (it!=end) cout<<*it++; } */ //当需要反复调用regex_search时,考虑使用这两个iterator,比较方便。 /*int main() { regex reg(" "); char* s="123 456 789 12345"; // cregex_token_iterator it; //This is correct, too. regex_token_iterator<char*> it(s,s+std::strlen(s),reg,-1); regex_token_iterator<char*> end; while (it!=end) { cout<<*it++<<endl; } return 0; } */
转载于:https://www.cnblogs.com/pcdelphi/archive/2009/07/05/2018015.html