C++ 在C+;中使用regex搜索.txt文件+;
我有一个未格式化的tweet列表(只是从网站整体粘贴的副本),我试图将每条tweet分离到各自的行中,同时删除文本文件中所有其他无关的细节C++ 在C+;中使用regex搜索.txt文件+;,c++,regex,twitter,bigdata,C++,Regex,Twitter,Bigdata,我有一个未格式化的tweet列表(只是从网站整体粘贴的副本),我试图将每条tweet分离到各自的行中,同时删除文本文件中所有其他无关的细节 我现在有一个正则表达式字符串,它在我在记事本++中搜索时是有效的,但是由于某种原因,我不能通过C++来工作。 我正在搜索的文本示例如下: Autotestdrivers.com @testdrivernews Nov 6 Tesla Model S third row of seats confuses,… http://dlvr.it/CgTbsL
我现在有一个正则表达式字符串,它在我在记事本++中搜索时是有效的,但是由于某种原因,我不能通过C++来工作。 我正在搜索的文本示例如下:
Autotestdrivers.com @testdrivernews Nov 6
Tesla Model S third row of seats confuses,… http://dlvr.it/CgTbsL #children #models #police #tesla #teslamotors #Autos #Car #Trucks
1 retweet 3 likes
Gina Stark ✈ @SuuperG Nov 6
Ha! Kinda. @PowayAutoRepair I have a thing for "long-nose" cars; #Porsche #Jaguar #Ferrari , and I love the lines of a #Tesla!
View conversation
0 retweets 2 likes
Tony Morice @WestLoopAuto Nov 6
\#WeirdCarNews via @Therealautoblog Tesla Model S third row of seats confuses, delights police http://www.autoblog.com/2015/11/06/tesla-model-s-third-row-seats-police/ …
View summary
0 retweets 0 likes
我使用的正则表达式以推文发布的日期和推文本身为准,如下所示:
[A-Z][a-z][a-z] \d+\r\n\r\n *.+\r\n
…但由于某些原因,我无法让它在我的代码中工作
#include <fstream>
#include <iostream>
#include <string>
#include <regex>
std::regex rgx("[A-Z][a-z][a-z]\\d+\\r\\n\\r\\n *.+\\r\\n");
std::string Location_Of_Tweet = "put location here";
std::smatch match;
std::cout << twitterFile;
std::ifstream twitterFiler;
twitterFiler.open(Location_Of_Tweet,std::ifstream::in);
const std::string tweetFile((std::istreambuf_iterator<char>(twitterFiler)), std::istreambuf_iterator<char>());
if (std::regex_search(tweetFile.begin(), tweetFile.end(), match, rgx))
{
std::cout << "Match\n";
for (auto m : match)
std::cout << " submatch " << m << '\n';
}
else
std::cout << "No match\n";
#包括
#包括
#包括
#包括
std::regex rgx(“[A-Z][A-Z][A-Z]\\d+\\r\\n\\r\\n*+\\r\\n”);
std::string Location\u Of_Tweet=“将位置放在这里”;
std::smatch匹配;
std::cout此正则表达式假定c++11正则表达式理解水平空格\h
如果没有,请将所有\h
替换为[^\S\r\n]
这被错误地解释为什么可能起作用。
但是,您需要一个更重要的分隔符来分隔tweet
“(?m)([A-Z][A-Z][A-Z]\\h+\\d+\\h*\\r?\\n\\s*^\\h*(?=\\s)(.+)”
解释
(?m) # Multi-line mode
( [A-Z] [a-z] [a-z] \h+ \d+ ) # (1), Date
\h* \r? \n \s* # Line break, any number of whitespace
^ \h* # Beginning of line
(?= \S ) # Next, first non-whitespace
( .+ ) # (2), Tweet
使用您的样本测试用例。
输出
你能详细解释一下“我不能让它在我的代码中工作”吗?具体问题是什么?编译器错误?运行时错误?意外的结果?请发帖,包括所有错误消息。抱歉,不清楚。代码是编译和运行的,我遇到的唯一问题是正则表达式没有找到任何匹配项(我知道在.txt文件中)。我编辑了代码以确保完整性。您使用的是哪种编译器和版本?例如,GCC4.8在std::regex
实现方面仍然存在问题。另外,如果其他人想要测试,您显示的示例代码甚至不会编译。我目前正在使用visual studio 2015创建并编译我的所有代码。您能发布一个您的正则表达式应该匹配的示例日期吗?您是否检查了线路终止符,有不同的约定(即*nix/linux上的\n
,Windows上的\r\n
,某些版本的MacOS上的iirc\n\r
)?
** Grp 1 - ( pos 37 , len 5 )
Nov 6
** Grp 2 - ( pos 46 , len 132 )
Tesla Model S third row of seats confuses,… http://dlvr.it/CgTbsL #children #models #police #tesla #teslamotors #Autos #Car #Trucks
-----------------
** Grp 1 - ( pos 226 , len 5 )
Nov 6
** Grp 2 - ( pos 235 , len 126 )
Ha! Kinda. @PowayAutoRepair I have a thing for "long-nose" cars; #Porsche #Jaguar #Ferrari , and I love the lines of a #Tesla!
-----------------
** Grp 1 - ( pos 435 , len 5 )
Nov 6
** Grp 2 - ( pos 444 , len 170 )
\#WeirdCarNews via @Therealautoblog Tesla Model S third row of seats confuses, delights police http://www.autoblog.com/2015/11/06/tesla-model-s-third-row-seats-police/ …