使用boost :: regex编译简单代码

时间:2012-07-15 06:11:55

标签: c++ boost boost-regex

我在编译以下代码时遇到问题:

#include <iostream>
#include <string>
#include <boost/regex.hpp>

using namespace std;

int main()
{
  string s;
  boost::regex re("^{(APPLE|ORANGE),(\\d*)}$");
  boost::cmatch matches;

  while(true)
  {
    cout << "String: ";
    cin >> s;
    if(boost::regex_match(s.c_str(), matches, re))
    {
      for(int i=1; i<matches.size(); i++)
      {
        string match(matches[i].first, matches[i].second);
        cout << "match[" << i << "]:  " << matches[i] << endl;
      }

    }
    else
    {
      cout << "no match" << endl;
    }
  }
  return 0;

}

我使用以下行来编译g ++:

g++ regexp_test.cpp -o regexp_test.o

也尝试过:

g++ -lboost_regex regexp_test.cpp -o regexp_test.o

但我收到这个长错误:

  

/tmp/ccyEpQIk.o:在函数bool boost::regex_match<char const*, std::allocator<boost::sub_match<char const*> >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)': regexp_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[bool boost::regex_match<char const*, std::allocator<boost::sub_match<char const*> >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)]+0x4c): undefined reference to boost :: re_detail :: perl_matcher&gt;中,boost :: regex_traits&gt; &GT; ::匹配()”   /tmp/ccyEpQIk.o:在函数boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)': regexp_test.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)]+0x22): undefined reference to boost :: basic_regex&gt;中&gt; :: do_assign(char const *,char const *,unsigned int)'   /tmp/ccyEpQIk.o:在函数boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, char const*)': regexp_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_]+0xb3): undefined reference to boost :: re_detail :: perl_matcher&gt;中,boost :: regex_traits&gt; &gt; :: construct_init(boost :: basic_regex&gt;&gt; const&amp;,boost :: regex_constants :: _ match_flags)'   collect2:ld返回1退出状态

我做错了什么?

2 个答案:

答案 0 :(得分:2)

也许你没有提升二进制文件,代码在这里用-lboost_regex编译。此外,花括号用于重复模式。例如\ d {3}表示三位数,因此您可能需要将正则表达式更改为:

boost::regex re("^(APPLE|ORANGE),(\\d*)$");

答案 1 :(得分:1)

下载boost库,然后只有-lboost_regex才能工作

如果你使用的是ubuntu,那么在终端输入它会安装所有的boost库。

'sudo apt-get install libboost-all-dev'

并按照perreal的说法进行更改。