如何提取特定符号后的数据

图片说明

就是将图片中 -a -b -c 后面的1 2 3 读取出来

            其中1 2 3 的长度不定,

使用正则表达式进行匹配,LZ可以看一下正则表达式的语法

 #include<iostream>
#include<fstream>
#include<string>
#include<regex>
using namespace std;
int main() {
    string str;
    int count = 0;
    ifstream in("in.txt");
    getline(in, str);       //读取第一行文本
    int result[1000];
    regex pat("-\\w{1,} (\\d{1,})");  //匹配模式
    smatch matches;                 //存储匹配到的字符串
    while (1) {
        if (!regex_search(str, matches, pat))   
            break;
        result[count++] = stoi(matches[1]);
        str=matches.suffix();       //修改剩余的字符串
    }
    for (int i = 0; i < count; i++) {
        cout << result[i] << " ";
    }
    return 0;
}

图片说明

这个文件中的-a,-b,-c后面能够确定都是数字吗?然后将-a,-b,-c后面的数字取出来? 你说的“其中1  2 3 长度不变”是什么意思?

  • 表明后面跟随指令 a 获取指令 ' ' 去除数据前面的空格 \w 获取后面的数据直到空格 继续第一个操作判断