我想使用boost正则表达式从文本文件中提取子匹配。目前我只返回第一个有效的行和完整的行,而不是有效的电子邮件地址。我尝试使用迭代器和使用submatches,但我没有成功。以下是当前代码:在C++中使用boost正则表达式提取子匹配
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
是正则表达式,谢谢。 – John