我正在尝试使用 boost regex 从文本文件中提取子匹配。目前我只返回第一个有效行和整行而不是有效的电子邮件地址。我尝试使用迭代器并使用子匹配,但我没有成功。这是当前代码:
if(Myfile.is_open()) {
boost::regex pattern("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$");
while(getline(Myfile, line)) {
string::const_iterator start = line.begin();
string::const_iterator end = line.end();
boost::sregex_token_iterator i(start, end, pattern);
boost::sregex_token_iterator j;
while ( i != j) {
cout << *i++ << endl;
}
Myfile.close();
}
原文由 John 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用 boost::smatch 。