使用 Boost 的 regex_match 编译 C++ 代码

Posted

技术标签:

【中文标题】使用 Boost 的 regex_match 编译 C++ 代码【英文标题】:Compiling C++ Code With Boost's regex_match 【发布时间】:2009-04-15 09:52:01 【问题描述】:

鉴于此代码:

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;


int main  ( int arg_count, char *arg_vec[] ) 
    if (arg_count !=2 ) 
        cerr << "expected one argument" << endl;
        return EXIT_FAILURE;
    

    string InputString = arg_vec[1];
    string toMatch = "FOO";

    const regex e(toMatch);
    if (const regex_match(InputString, e)) // this is line 26
        cout << "Match" << endl;
     else 
        cout << "No Match" << endl;
    


    return 0;

使用此命令编译:

g++ -I /home/foolishbrat/.boost/include/boost-1_37 test.cc -o test

它给出了这个错误:

test.cc: In function `int main(int, char**)':
test.cc:26: error: syntax error before `)' token

我的代码有什么问题?

如果我删除第 26 行中的“const”并使用相同的命令进行编译。 我会得到这个错误:

/misc/raid2/Melchior/foolishbrat/tmp/ccr3zzcH.o(.gnu.linkonce.t._ZN5boost11regex_matchIN9__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_flagsE+0x61): In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
/misc/raid2/Melchior/foolishbrat/tmp/ccr3zzcH.o(.gnu.linkonce.t._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j+0x27): In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned)':
: undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned)'
/misc/raid2/Melchior/foolishbrat/tmp/ccr3zzcH.o(.gnu.linkonce.t._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC1ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_+0xf2): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher[in-charge](__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >)':
: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: ld returned 1 exit status

更新:

通过此编译和链接解决:

g++ -I ~/.boost/include/boost-1_37 test.cc -o test  /home/foolishbrat/.boost/lib/libboost_regex-gcc33-mt-1_37.a

【问题讨论】:

【参考方案1】:

您需要链接必要的 boost 库。 在构建命令中添加-l &lt;path to libraries&gt;

【讨论】:

【参考方案2】:
if (const regex_match(InputString, e)) // this is line 26

您不需要(实际上也不应该)在这里说const。只要去掉这个词,至少这个错误应该会消失。

【讨论】:

通常,编译器/链接器错误的数量并不代表您的进度。您可能会修复一个问题,但会得到比以前更多的错误,因为编译现在可以继续进行。【参考方案3】:

删除第 26 行的 const 关键字。

【讨论】:

以上是关于使用 Boost 的 regex_match 编译 C++ 代码的主要内容,如果未能解决你的问题,请参考以下文章

boost regex expression

使用 or 匹配正则表达式

boost库之正则表达式regex

错误:在 gcc 5.2.0 中使用已删除的函数 bool regex_match

在 Redshift 中使用 regex_match 的问题

boost 1.56.0 编译及使用