// RegExDemo.cpp

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

using namespace std;

const string validator = "^([a-zA-z0-9 _\\-]+=[a-zA-z0-9 _\\-]+;?)+$";
const string default_validator = "^([a-zA-z0-9 _\\-]+=[a-zA-z0-9 _\\-]+;?)+$";

bool validate(const std::string& str)
{
  boost::regex e;

  try {
    e.assign(validator);
  }
  catch (const boost::bad_expression& e) {
    try {
      cout << "Validator expression is not valid. Trying the default validator expression" << endl;
      e.assign(default_validator);
    }
    catch (const boost::bad_expression& e) {
      cout << "Even the default validator expression is not valid." << endl;
      return false;  // even the default validator is invalid
    }
  }

  boost::smatch matches;
  return regex_search(str, matches, e);
}

int main()
{
  cout << validate("a=b") << endl;
  cout << validate("a=b;c=d") << endl;
  cout << validate("a=b;c=d;") << endl;
  cout << validate("123==32;") << endl;
  cout << validate("123=32;;") << endl;
  cout << validate("c=COUNTRY;city=CITY;email=SMTP;l=DIVISION;ou=COMPANY;postalcode=ZIP;st=STATE;") << endl;
}