// 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 exp;

  try {
    exp.assign(validator);
  }
  catch (const boost::bad_expression& e) {
    try {
      cout << "Validator expression is not valid. Trying the default validator expression" << endl;
      exp.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, exp);
}

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;homephone=PRIVTEL;l=DIVISION;mobile=GSM;ou=COMPANY;postalcode=ZIP;preferredlanguage=LANGUAGE_ID;st=STATE;street=ADDR1;xprCostInfo=COSTINFO;xprFaxHeader=FAXG3 ID;xprPin=PIN;") 
  << endl;
}