美文网首页
23.9使用正则表达式进行模式匹配

23.9使用正则表达式进行模式匹配

作者: Jianbaozi | 来源:发表于2019-10-07 17:12 被阅读0次
#include<iostream>
#include<regex>
#include<string>
#include<cstring>
#include<fstream>
using namespace std;
template<typename T>T from_string(const string& s){
    istringstream is{s};
    T t;
    is>>t;
    return t;
}
int main(){
    ifstream ifs{"D:\\Codes\\test\\data.txt"};
    if(!ifs)
    cout<<"Open file error!\n";
    cout<<"Open file success!\n";
    string line;
    int lineno=0;
    regex header{R"(^[\w ]+(    [\w ]+)*$)"};
    regex row{R"(^[\w ]+(   \d+)(   \d+)(   \d+)$)"};
    if(getline(ifs,line)){
        smatch matches;
        if(!regex_match(line,matches,header))
            cout<<"No header!\n";
        cout<<matches[0]<<'\n';
    }
    int boys=0;
    int girls=0;
    while(getline(ifs,line)){
        ++lineno;
        smatch matches;
        if(!regex_match(line,matches,row))
        cout<<"bad line!\n";
        int curr_boy=from_string<int>(matches[1]);
        int curr_girl=from_string<int>(matches[2]);
        int curr_total=from_string<int>(matches[3]);
        cout<<"line "<<lineno<<":\t"<<curr_boy<<" "<<curr_girl<<" "<<curr_total<<'\n';
        if(curr_total!=curr_boy+curr_girl)
            cout<<" bad row sum at"<<lineno<<'\n';
            string s=matches[0];
        if(s.substr(0,12)=="Alle klasser"){
            cout<<"Total line find!\n";
            if(curr_boy!=boys)  cout<<"boys don't add up!\n";
            if(curr_girl!=girls)    cout<<"girls don't add up!\n";
            if(!(ifs>>ws).eof())    cout<<"characters after total line\n";
            return 0;
        }
        boys+=curr_boy;
        girls+=curr_girl;
    }
    cout<<"didn't find total line!\n";
    system("pause");
    return -1;
}

测试文件

KLASSE  ANTAL DRENGE    ANTAL PIGER ELEVER IALT
0A  12  11  23
1A  7   8   15
1B  4   11  15
2A  10  13  99999s
3A  10  12  22
4A  7   7   14
Alle klasser    50  62  112
blah..blah

输出结果

D:\Codes\test>a.exe
Open file success!
KLASSE  ANTAL DRENGE    ANTAL PIGER     ELEVER IALT
line 1: 12 11 22
 bad row sum at1
line 2: 7 8 15
line 3: 4 11 15
line 4: 10 13 23
line 5: 10 12 22
line 6: 7 7 14
line 7: 50 62 112
Total line find!
characters after total line

D:\Codes\test>a.exe
Open file success!
KLASSE  ANTAL DRENGE    ANTAL PIGER     ELEVER IALT
line 1: 12 11 23
line 2: 7 8 15
line 3: 4 11 15
line 4: 10 13 99999
 bad row sum at4
line 5: 10 12 22
line 6: 7 7 14
line 7: 50 62 112
Total line find!
characters after total line

相关文章

  • 23.9使用正则表达式进行模式匹配

    测试文件 输出结果

  • 树莓派——mysql的学习(3)

    由MySQL提供的模式匹配的其它类型是使用扩展正则表达式。当你对这类模式进行匹配测试时,使用REGEXP和NOT ...

  • mysql(04day)

    Mysql支持正则表达式的匹配,Mysql中使用REGEXP操作符来进行正则表达式匹配。 下面中的正则模式中可用于...

  • 第10章 使用stringr处理字符串P2

    第10章 使用stringr处理字符串 要到正则表达式了!紧不紧张!刺不刺激 :) 使用正则表达式进行模式匹配 准...

  • MySQL 正则表达式

    MySQL中使用 REGEXP 操作符来进行正则表达式匹配。 1、正则模式描述 ^:匹配输入字符串的开始位置。如果...

  • JavaScript正则表达式全解

    正则表达式 reg.test(str) 模式 i 使用构造函数创建正则表达式 模式 g g 表示全局匹配 str....

  • 正则表达式

    正则表达式使用技巧 1.定义匹配模式var pattern = /[0-9]+/; //这是一个数字匹配模式,需匹...

  • python基础—正则表达式即re模块!

    正则表达式(regular expression),就是字符匹配模式,而这个匹配规则在我们写爬虫进行数据提取,或者...

  • 【读书笔记】_正则表达式

    1、正则表达式使用的特殊符号和字符 1.1、用管道符号( | )匹配多个正则表达式模式,即或,匹配多个表达式。 例...

  • 正则表达式

    概念 正则表达式是用于字符串匹配规则的工具 规则 不使用元字符 可以不使用元字符,直接匹配。如模式hi,匹配所有连...

网友评论

      本文标题:23.9使用正则表达式进行模式匹配

      本文链接:https://www.haomeiwen.com/subject/nejoyctx.html