1、编译器:vs2003
2、语言:C++
3、正则表达式库:boost
4、程序要求:
对于土豆网的一个视频页面地址(eg:http://www.tudou.com/playlist/p/a67087i94457134.html),能够提取出来vid,即94457134
5、程序代码:
string s="input the web site:\n";
cout<<s<<endl;
string url;
getline(cin,url);
cout<<"you enter the string is :"<<url<<endl;
string regstr="^.+?/p/a\\d+i(\\d+)\\.html";
regex expre(regstr);
smatch what;
if(regex_match(url,what,expre))
{
cout<<"match"<<endl;
for(int i=0;i<what.size();i++)
cout<<"str:"<<what[i].str()<<endl;
}
else
{
cout<<"Not match"<<endl;
}
注意一点:对于在c++中使用正则表达式来说,对于反斜杠\,必须在前面在加一个反斜杠,例如:如果想表达C:\temp,则正则表达式要写成C:\\temp,进一步在c++中要写成C:\\\\temp。