1

I'm having trouble compiling the following code:

#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;

}

I used the following line to compile with g++:

g++ regexp_test.cpp -o regexp_test.o

also tried:

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

but I'm getting this long error:

/tmp/ccyEpQIk.o: In function 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 toboost::re_detail::perl_matcher >, boost::regex_traits > >::match()' /tmp/ccyEpQIk.o: In function 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 toboost::basic_regex > >::do_assign(char const*, char const*, unsigned int)' /tmp/ccyEpQIk.o: In function 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 toboost::re_detail::perl_matcher >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)' collect2: ld returned 1 exit status

what am I doing wrong?

2 Answers 2

3

Perhaps you don't have the boost binaries, the code compiles here with -lboost_regex. Also, curly brackets are for repeating patterns. For example \d{3} means three digits so you may need to change your regex to:

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

Sign up to request clarification or add additional context in comments.

Comments

1

Download the boost libraries then only -lboost_regex will work

if you are using ubuntu then type this in terminal it will install all the boost libraries.

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

and do the changes as said by perreal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.