그래서 많은 다른 텍스트 파일로 전자 메일을 보내고 레이아웃에서 일관성이없는 파일에서 전자 메일을 추출해야합니다. Boost::Regex
및 Boost::File-system
을 사용하여 읽은 다음 전자 메일 주소를 추출합니다. 그러나 이메일을 찾거나 추출하는 데는 효과가없는 것 같습니다. 이 단어는 email
또는 a
과 같은 간단한 단어와 일치 할 수 있습니다. 하지만 실제로 파일을 읽는 데 문제가있는 것 같습니다. 다음과 같이텍스트 파일의 Boost :: Regex를 사용하여 하위 문자열 추출
최소한의 예입니다 (더는 포함되지 않습니다) :
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <boost/regex.hpp>
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem; // File system is namespace.
int main() {
boost::regex pattern("\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,}\b"); // Email regex to match.
boost::smatch result;
fs::path targetDir(boost::filesystem::current_path()); // Look in this folder.
fs::directory_iterator it(targetDir), eod; // Iterate over all the files in said directory.
std::string line;
BOOST_FOREACH(fs::path const &p, std::make_pair(it, eod)) { // Actual iteration.
if (fs::is_regular_file(p)) { // What this does is checks if it's a normal file.
std::ifstream infile(p.string()); // Read file line by line.
if (p.string().substr(p.string().length() - 3) != "txt") {
continue; // Skip to next file if not text file.
}
while (std::getline(infile, line)) {
bool isMatchFound = boost::regex_search(line, result, pattern);
if (isMatchFound)
{
for (unsigned int i = 0; i < result.size(); i++)
{
std::cout << result[i] << std::endl;
}
}
}
infile.close();
}
}
return 0;
}
가 작동하지 않는 이유를 잘 모르겠어요 :
"[email protected]","S"
"[email protected]","R"
[email protected]<br>
그리고 다음과 같이 이메일의 샘플이 될 수 있습니다 전자 메일이 텍스트 파일에있을 수있는 다양한 방법,이 정규식을 어떻게 얻을 수 있습니까?