美文网首页
23.8.7正则表达式错误

23.8.7正则表达式错误

作者: Jianbaozi | 来源:发表于2019-10-07 17:12 被阅读0次
#include<regex>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;
int main() {
    cout << "Input pattern: ";
    string pat;
    regex pattern;
    getline(cin,pat);
    try{
        pattern=pat;
        cout<<"Pattern is: "<<pat<<'\n';
    }catch(...){  //bad_expression 编译报错 
                      //gcc version 5.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
        cout<<pat<<" is not a valid regular expression.";
        exit(1);
    }
    cout<<"Enter lines:\n";
    int lineno=0;
    for(string line;getline(cin,line);){
        ++lineno;
        smatch matches;
        if(regex_search(line,matches,pattern)){
            cout<<"line "<<lineno<<": "<<line<<'\n';
            for(int i=0;i<matches.size();++i){
                cout<<"\tmatches["<<i<<"]"<<matches[i]<<'\n';
            }
        }
        else
            cout<<"line "<<lineno<<": "<<line<<'\n'<<"didn't match!"<<endl;        
        }
    system("pause");
    return 0;
}

编译提示:

D:/mingw64/x86_64-w64-mingw32/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
test.cpp: In function 'int main()':
test.cpp:10:5: error: 'regex' was not declared in this scope
     regex pattern;
     ^
test.cpp:11:17: error: 'pattern' was not declared in this scope
     getline(cin,pattern);
                 ^
test.cpp:15:12: error: 'bad_expression' does not name a type
     }catch(bad_expression){
            ^

测试:

PS D:\Codes\test> g++ test.cpp -std=c++11   
PS D:\Codes\test> ./a.exe
Input pattern: \w{3}(\d{3})
Pattern is: \w{3}(\d{3})
Enter lines:
abc123
line 1: abc123
        matches[0]abc123
        matches[1]123   
456def
line 2: 456def
didn't match! 
aabbcc789
line 3: aabbcc789       
        matches[0]bcc789
        matches[1]789   

相关文章

  • 23.8.7正则表达式错误

    编译提示: 测试:

  • pipeline报org.jenkinsci.plugins.s

    今天在pipeline中使用正则表达式,构建pipeline报如下错误: 原因:Jenkins的Script Se...

  • Date对象 ios出现NaN错误

    日期'-'模式在ios中不能使用,出现NaN(Not a Number)错误,转换为'/'模式即可 正则表达式转换:

  • 2020-07-20 JS学习 表单验证

    根据正则表达式的要求,表单内容输入正确或错误在输入框后面都有相应的提示。信息错误时点击提交按钮提交失败,光标返回用...

  • react-native node.js版本问题

    node.js在使用12版本是,运行react-native start报错,显示正则表达式错误,卸载安装后使用1...

  • 零零杂杂的学习记录

    使用re正则表达式匹配时的错误 出错的主要原因是因为: Python中append与extend的用法区别 app...

  • Linux命令行与Shell脚本编程大全-shell正则表达式

    本章内容: 定义正则表达式 了解基本正则表达式 扩展正则表达式 创建正则表达式 定义正则表达式 正则表达式是你定义...

  • 正则相关

    正则表达式基本语法 正则表达式常见字符 正则表达式特殊字符 正则表达式数量词 正则表达式边界匹配 正则表达式逻辑或...

  • 正则表达式系列-1

    正则表达式系列-1正则表达式系列-2正则表达式系列-3正则表达式系列-4 什么是正则表达式 正则表达式就是用事先定...

  • PHP想入门? 看这个就够了

    本文将介绍PHP的一些较重要的内容。包括:面向对象编程、正则表达式、程序错误处理、XML、AJAX、图像处理、My...

网友评论

      本文标题:23.8.7正则表达式错误

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