在C++中使用正则表达式搜索.txt文件
Searching through .txt files using regex in C++
我有一个未格式化的推文列表(只是从网站上复制粘贴),我试图将每条推文分成单独的行,同时删除文本文件中所有其他无关的细节。
我目前有一个正则表达式字符串,当我在记事本++中搜索时可以工作,但由于某种原因,我无法让它通过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+rnrn *.+rn
。但由于某种原因,我无法让它在我的代码中工作。
#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 << "Matchn";
for (auto m : match)
std::cout << " submatch " << m << 'n';
}
else
std::cout << "No matchn";
这个正则表达式假设 c++11 正则表达式理解水平空格h
。
如果没有,请将所有h
替换为 [^Srn]
。
这被模糊地解释为可能有效的方法。
但是,您需要一个更实质性的分隔符来分隔推文。
"(?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
使用示例测试用例。
输出
** 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/ …
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 如何将 txt 文件中的行分隔为组件C++
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 在树结构.txt文件中搜索节点
- CMakeList 中应包含哪些模块.txt以进行近似最近邻搜索?
- 如何使用 C++ 在 txt 文件中搜索字符
- 在 txt 文件中搜索中间子字符串未被选中的字符串
- 如何搜索txt文件以获取正则
- C 搜索.txt文件以查看字符串的所有出现并列出所有结果
- 在 c++ 中搜索和替换 txt 文件中的字符串
- C++fstream在.txt中搜索一个结构
- 在 c++ 中搜索.txt文件只会得到第一行
- C++从.txt文件中读取+二进制搜索+排序;上课的麻烦
- 标志控制,而循环搜索txt文件
- 在C++中使用正则表达式搜索.txt文件
- C++ 搜索 TXT 文件返回 int.
- 优化.txt文件中的字符串搜索
- C++正在将行搜索到txt文件中
- 根据ID/Registration搜索和编辑.txt fstream文件