• 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
    
  • 相关阅读:
    Java8新特性简介
    责任链模式
    Bean的生命周期
    APP中https证书有效性验证引发安全问题(例Fiddler可抓https包)
    程序员成长指南
    Go 代码性能优化小技巧
    Go slice 扩容机制分析
    一次 Go 程序 out of memory 排查及反思
    curl 常用操作总结
    Go benchmark 详解
  • 原文地址:https://www.cnblogs.com/rootshaw/p/12910684.html
Copyright © 2020-2023  润新知