C++正则表达式错误

 手机用户2502890731 发布于 2023-02-09 18:53

我一直在使用C++中的正则表达式,但遇到了一些错误:

这是我的剧本

#include 
#include 
using namespace std;

string input(string prompt)
{
    cout << prompt;
    string str;
    cin >> str;
    return str;
}

int main() {
    string str;
    while (true) {

        str = input("Enter some text: ");
        regex e("([:w:])+", regex_constants::icase);

        bool match = regex_match(str, e);

        cout << (match? "Matched" : "Not matched") << endl;
    }
}

当我编译它并运行(g++ -std=c++11 test.cpp && ./a.out)时,我收到以下错误:

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

是什么造成的?有没有办法解决它?

GCC版本: gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)


好.我试过boost.regex(没有运气).您可以在此处找到该文件:http://www.mitchr.me/SS/exampleCode/boost/regexExample1.cpp.html

以下是来自的输出g++ test.cpp && ./a.out:

/tmp/ccq38Rqf.o: In function `char boost::re_detail::global_lower(char)':
test.cpp:(.text._ZN5boost9re_detail12global_lowerIcEET_S2_[_ZN5boost9re_detail12global_lowerIcEET_S2_]+0x14): undefined reference to `boost::re_detail::do_global_lower(char)'
/tmp/ccq38Rqf.o: In function `char boost::re_detail::global_upper(char)':
test.cpp:(.text._ZN5boost9re_detail12global_upperIcEET_S2_[_ZN5boost9re_detail12global_upperIcEET_S2_]+0x14): undefined reference to `boost::re_detail::do_global_upper(char)'
/tmp/ccq38Rqf.o: In function `bool boost::regex_match >, char, boost::regex_traits > >(char const*, char const*, boost::match_results > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags)':
test.cpp:(.text._ZN5boost11regex_matchIPKcSaINS_9sub_matchIS2_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SA_RNS_13match_resultsISA_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIPKcSaINS_9sub_matchIS2_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SA_RNS_13match_resultsISA_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x77): undefined reference to `boost::re_detail::perl_matcher >, boost::regex_traits > >::match()'
/tmp/ccq38Rqf.o: In function `boost::basic_regex > >::assign(char const*, char const*, unsigned int)':
test.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
/tmp/ccq38Rqf.o: In function `boost::re_detail::perl_matcher >, boost::regex_traits > >::perl_matcher(char const*, char const*, boost::match_results > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags, char const*)':
test.cpp:(.text._ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ES3_S3_RNS_13match_resultsIS3_S6_EERKNS_11basic_regexIcSA_EENS_15regex_constants12_match_flagsES3_[_ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ES3_S3_RNS_13match_resultsIS3_S6_EERKNS_11basic_regexIcSA_EENS_15regex_constants12_match_flagsES3_]+0xf6): undefined reference to `boost::re_detail::perl_matcher >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)'
/tmp/ccq38Rqf.o: In function `bool boost::regex_search >, char, boost::regex_traits > >(char const*, char const*, boost::match_results > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags, char const*)':
test.cpp:(.text._ZN5boost12regex_searchIPKcSaINS_9sub_matchIS2_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SA_RNS_13match_resultsISA_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsESA_[_ZN5boost12regex_searchIPKcSaINS_9sub_matchIS2_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SA_RNS_13match_resultsISA_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsESA_]+0xa2): undefined reference to `boost::re_detail::perl_matcher >, boost::regex_traits > >::find()'
/tmp/ccq38Rqf.o: In function `bool boost::regex_search<__gnu_cxx::__normal_iterator, std::allocator > >, char, boost::regex_traits > >(__gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator, boost::match_results<__gnu_cxx::__normal_iterator, std::allocator > > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator)':
test.cpp:(.text._ZN5boost12regex_searchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsESD_[_ZN5boost12regex_searchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsESD_]+0xa2): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator, std::allocator > >, boost::regex_traits > >::find()'
/tmp/ccq38Rqf.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator, std::allocator > >, boost::regex_traits > >::perl_matcher(__gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator, boost::match_results<__gnu_cxx::__normal_iterator, std::allocator > > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator)':
test.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_]+0x116): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator, std::allocator > >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)'
/tmp/ccq38Rqf.o: In function `boost::re_detail::basic_regex_formatter, boost::match_results<__gnu_cxx::__normal_iterator, std::allocator > > >, boost::regex_traits_wrapper > >, char const*>::toi(char const*&, char const*, int, mpl_::bool_ const&)':
test.cpp:(.text._ZN5boost9re_detail21basic_regex_formatterINS0_19string_out_iteratorISsEENS_13match_resultsIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS9_EEEEENS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEES8_E3toiERS8_S8_iRKN4mpl_5bool_ILb1EEE[_ZN5boost9re_detail21basic_regex_formatterINS0_19string_out_iteratorISsEENS_13match_resultsIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS9_EEEEENS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEES8_E3toiERS8_S8_iRKN4mpl_5bool_ILb1EEE]+0x31): undefined reference to `boost::cpp_regex_traits::toi(char const*&, char const*, int) const'
collect2: error: ld returned 1 exit status

Cubbi.. 6

直到几个月前,GCC中没有实现(尽管它的一些功能会在没有警告的情况下编译):http://gcc.gnu.org/bugzilla/show_bug.cgi?id = 53631#c17

要试验TR1/C++ 11正则表达式,您需要使用boost.regex,gcc 4.9或其他标准库实现(例如libc ++)

1 个回答
  • <regex>直到几个月前,GCC中没有实现(尽管它的一些功能会在没有警告的情况下编译):http://gcc.gnu.org/bugzilla/show_bug.cgi?id = 53631#c17

    要试验TR1/C++ 11正则表达式,您需要使用boost.regex,gcc 4.9或其他标准库实现(例如libc ++)

    2023-02-09 18:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有