私は多くの異なるテキストファイルで電子メールを持っており、レイアウトから一貫していないファイルからそれらを抽出する必要があります。私は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>
そして、電子メールがテキストファイル内にあることができるさまざまな方法、この正規表現をどのようにして一致させるのですか?