zoukankan      html  css  js  c++  java
  • c++ regex 正则类例子及其gcc4.8报错

    参考:c++ regex类


    例子

    #include <iostream>
    #include <string>
    #include <regex>
    using namespace std;
    int main(void) 
    {
    	cout << "hello world" <<endl;
    	std::string str("zh_CN");
    	std::string pref ("zh-CN");
    	cout << "str: "<< str <<endl;
    	cout << "perf: " <<pref <<endl;
    
    	cout << "compare: "<<str.compare("zh")<<endl;
    	
    	size_t sep = str.find ('_');
    	std::regex locale(str.substr(0,sep)+ "-[A-Za-z]*"  +str.substr(sep+1));	 //new 一个正在
    	
    	cout << "create locale ok"<<endl;
    	cout << "is same: "<< std::regex_match("zh-CN",locale) << endl;
    	return 0;
    }
    

    报错记录
    1.使用g++ 编译 g++ -o regex regex.cpp

     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
    

    解决:是由于新版c++11 需要 -std=c++11

    2.使用 g++ -o regex regex.cpp -std=c++11
    ./regex 后出现段错误

    terminate called after throwing an instance of 'std::regex_error'
      what():  regex_error
    Aborted (core dumped)
    

    查网上原因是因为gcc 4.8 对新特性c++11的regex不完全支持
    见参考Is gcc 4.8 or earlier buggy about regular expressions?

    我的g++ 版本

    g++ --version
    g++ (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
    Copyright (C) 2013 Free Software Foundation, Inc.

    更换g++ 4.9试试 就可以了

    /usr/bin/g++-4.9 -o regex regex.cpp -std=c++11
    ~/test_2020.01.07/c++_func$ ./regex 
    hello world
    str: zh_CN
    perf: zh-CN
    compare: 3
    create locale ok
    is same: 1
    
  • 相关阅读:
    media query不一致
    数据库设计三范式
    异步概念及使用场景
    关于webservice框架CXF的总结
    通过bash文件(shell命令)对文件进行修改
    shell命令相关问题
    shell对文本进行操作命令
    虚拟机安装系统常见问题
    安装autotools系列工具
    Centos和Ubuntu下打包项目
  • 原文地址:https://www.cnblogs.com/rootshaw/p/12910684.html
Copyright © 2011-2022 走看看