您的位置:首页 > 其它

boost 库 正则 使用一则 -------- 歌词文件信息提取

2012-07-20 12:50 225 查看
说明参考注释.....





#include <iostream>
#include <fstream>
#include <string>
#include <boost/regex.hpp>

using namespace std;

#define BUFFER_SIZE 1024

char buffer[BUFFER_SIZE];

//下边这个正则有4个匹配项,第一个是匹配字串的全部,第二个是<time>第三个是(\.\d+),即百分秒,第四个是歌词信息
boost::regex reg_lyricItem("(?<time>\\[\\d\\d\\:\\d\\d(\\.\\d+)?\\])(?<lyric>[^\\n\\[]+)?");

int main()
{
ifstream fs("riya - 小さなてのひら.txt",ios::in);
cout<<fs.good()<<endl;
string line;
// 一行一行取
while(fs.getline(buffer,BUFFER_SIZE))
{
// 一行里可能有多句
line = string(buffer);
// 关键数据结构 变量定义

//存放结果
boost::match_results<std::string::const_iterator> result;
//迭代器
std::string::const_iterator begin = line.begin(),end = line.end();
while(boost::regex_search(
begin,
end,
result,
reg_lyricItem,
boost::match_default
)
)
{
//可以通讯result
.matched来查看这一部分是否被匹配,如果匹配再进行相应处理也不迟,这里简化没有判断.
cout<<string(result[1].first,result[1].second)<<" "<<string(result[3].first,result[3].second)<<endl;
//假设这一行有多句,接着上句的最后向后分析
begin = result[0].second;
}
}
cout<<fs.rdstate();
return 0;
}


本文出自 “冰狐浪子的博客” 博客,请务必保留此出处http://bhlzlx.blog.51cto.com/3389283/936179
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: