如何从txt文件c++中提取括号之间的数字
How can I extract numbers between a parenthesis from a txt file c++?
我的问题是:我的逻辑有一些问题。我已经检测到它何时有括号,但现在我需要找到数字,并知道它们在txt文件中重复了多少次。这是我的txt文件:
(Visual basic)
(Llorente)
(Porto, 2008)
(Sommerville, 2010)
References
Visual Basic. (s.f.). Navarra.
Llorente, P.B. (s.f.). Fortran.
Porto, J.P. (2008)
Sommerville, I. (2010). Software Engineering. Unite Kingdom: Pearson.
结果应该是:年份:2008-2次,年份:2010-2次等。PD:谢谢,我很无聊。
#include <regex>
#include <iostream>
#include <fstream>
#include <map>
//33
int main()
{
std::ifstream readStream("references.txt");
std::map<int, int> cMap;
std::string input;
std::regex reg(R"(([a-zA-Zs]*,?s*([0-9]+)))");
std::regex inte("(\+|-)?[[:digit:]]+");
///333
while (std::getline(readStream, input)) {
std::match_results < std::string::const_iterator > m;
if ((std::regex_search(input, m, reg)) ) {
int year = std::stoi(m[1]);
auto value = cMap.find(year);
if (value != cMap.end()) {
cMap[value->first] = value->second + 1;
} else {
cMap[year] = 1;
}
}
}
//33
for (auto x : cMap) {
std::cout << "year " << x.first << " is - " << x.second << " times." << std::endl;
}
//3
return 0;
}
您可以使用std::regex_search
方法来匹配行。但请记住在文件顶部包含#include <regex>
。
#include <regex>
#include <iostream>
#include <fstream>
#include <map>
int main()
{
std::ifstream readStream("path/to/your/Example.txt");
std::map<int, int> countMap; // Map containing (year, count) value pairs.
std::string in;
std::regex reg(R"(([a-zA-Zs]*,?s*([0-9]+)))"); // For the regex: ([a-zA-Zs]*,?s*([0-9]+))
while (std::getline(readStream, in)) {
std::match_results < std::string::const_iterator > m;
if (std::regex_search(in, m, reg)) { // If the current line matches our regex pattern.
int year = std::stoi(m[1]); // The second element of the array contains our string representing the year.
auto val = countMap.find(year);
if (val != countMap.end()) { // If the year is already in the countMap we want to increment it.
countMap[val->first] = val->second + 1;
} else {
countMap[year] = 1; // year doesn't exist in the countMap, it is the first time.
}
}
}
for (auto x : countMap) { // x is of type std::pair<int, int> which is our (year, count) value pair
std::cout << "year " << x.first << " is - " << x.second << " times." << std::endl;
}
return 0;
}
对于OP的任务,一个简单的循环可以完成遍历字符串中所有字符的工作。从而管理两种状态:
open
。。。true
在'('
之后和')'
之前,否则为falsenumber
。。。CCD_ 8,而CCD_ 9是CCD_
当number
是true
时,收集数字并组合为int
。如果在number
为true
时找到非数字,则这是数字的末尾,并存储该值。
号码的检查也可以在行尾完成。在这种特定情况下,这意味着语法错误,因为这意味着缺少右括号()
(。(OP没有说明如何处理。(
我的样本代码:
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
std::vector<int> parseLine(const std::string &text)
{
bool open = false, number = false;
std::vector<int> values; int value = 0;
for (char c : text) {
switch (c) {
case '(': open = true; break;
case ')': open = false;
default:
if (open && c >= '0' && c <= '9') {
number = true;
(value *= 10) += (c - '0'); // value = 10 * value + (c - '0')
} else if (number) {
values.push_back(value);
value = 0; number = false;
}
}
}
if (number) {
values.push_back(value);
std::cerr << "ERROR: Missing ')' at line end!n";
}
return values;
}
const std::string text =
"(Visual basic)n"
"(Llorente)n"
"(Porto, 2008)n"
"(Sommerville, 2010)n"
"Referencesn"
"Visual Basic. (s.f.). Navarra.n"
"Llorente, P.B. (s.f.). Fortran.n"
"Porto, J.P. (2008)n"
"Sommerville, I. (2010). Software Engineering. Unite Kingdom: Pearson.n";
int main()
{
// open file
std::istringstream in(text);
// parse/process file line by line
std::map<int, int> histo;
for (std::string line; std::getline(in, line);) {
const std::vector<int> values = parseLine(line);
// count occurrences of values
for (int value : values) ++histo[value];
}
// report
std::cout << "Report:n";
for (const std::pair<int, int> &entry : histo) {
std::cout << entry.first << ": " << entry.second << 'n';
}
// done
return 0;
}
输出:
Report:
2008: 2
2010: 2
coliru上的实时演示
相关文章:
- 在数字之间插入 + 或 - 符号以使其等于整数
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 获取 2 个数字之间的差异百分比
- 两个有符号数字之间的距离
- 无法理解代码背后的逻辑,这是在两个给定数字之间生成素数的优化问题
- C++:这两种将数字写入矩阵的方式之间是否存在显着的速度差异?
- 如何从txt文件c++中提取括号之间的数字
- 正则表达式匹配字符串之间的数字
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- 数字之间的比较
- 在没有比较运算符的情况下查找 2 个数字之间的最小值
- 在[a,b]间隔之间找到相同的数字
- 不理解连续数字之间的空格代码
- 如果 2 个数字之间的差值在 1 到 100 之间,则应运行 if 语句
- 第一个和第二个数字之间能被 4 和 6 整除的所有整数的总和
- 0 和 n 之间有多少个数字满足按位属性 i&y==i?
- 2 个给定数字之间的双精度密度
- 三个数字之间的 C++ 相加两个更高的数字,没有循环和数组
- 我如何生成2D数组,在行和列之间具有不同的数字
- 给定n个数字,编写一个例程,以在4个连续数字之间找到最大的数字