c++ - 从已知大小和一些索引字符的文件中搜索字符串
问题描述
我想从匹配某些条件的文件中查找字符串。我知道字符串的大小和一些位置字符。因此,据我所知,最好的解决方案是创建正则表达式并搜索文件中的字符串。
我创建了以下正则表达式来搜索所有可能的字符:
[a-zA-Z0-9#&()\.\-/=?%@!\^\$\\ \.\*\+\?\(\)\[\]\{\}\|]
在我使用上述正则表达式编写的代码下方:
int main()
{
std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char(t)),std::istreambuf_iterator<char>());
smatch m;
string allchar = "[a-zA-Z0-9#&()\.\-/=?%@!\^\$\\ \.\*\+\?\(\)\[\]\{\}\|]";
//String of size five, have S at 0th index and B at last index
string search = "S"+ allchar + allchar + allchar +"B";
regex r(search);
regex_search(str, m, r);
for (auto x : m)
{
cout << x << endl;
}
}
但是使用上面的代码我没有得到我应该得到的正确答案。我认为我在创建全字符字符串时犯了一些错误,但无法识别我的错误。
解决方案
推荐阅读
- mongodb - MongoDB,在一行中显示两个发现
- gremlin - Gremlin:如何识别哪些属性属于哪些边
- postgresql - Postgres 远程连接 - Windows 服务器
- sql - Spark,基于第二个数据集的 SQL 聚合
- google-chrome - 如何在 Chrome 中消除页面的多余空间?
- javascript - 如何处理 Vue.js 中数据中多层 JSON 的设置值
- javascript - 如果失去保真度,如何解析 JSON 并抛出?
- python-3.x - scipy.stats 转换器正常 z 分数到 p 值 Python3
- java - Apache骆驼上的相互TLS
- python-3.x - 将秒的numpy数组转换为分钟和秒