c/c++

정규식 표현

밀크빵 2013. 3. 27. 16:11

#include <iostream>

#include <string>

#include <boost/regex.hpp>  // Boost.Regex lib

#include <fstream>


using namespace std;

const int BUFSIZE = 10000;


int main(int argc, char ** argv ) 

{

string s, sre;

boost::regex re;

boost::cmatch matches;


cout << "Expression : ";

cin >> sre;


cout <<"str: ";

cin >> s;


try

{

re = sre;

}

catch(boost::regex_error& e)

{

cout << sre << "is not a valid regular expression : " << e.what() << endl;

// continue;

}

if(boost::regex_match(s.c_str(), matches, re))

{

for(int i = 0; i<matches.size(); i++)

{

string match(matches[i].first, matches[i].second);

cout <<"\tmatches[" <<i << "] \ " <<match <<endl;

}

}

else

{

cout << "The regexp \"" << re << "\" does not mach \"" << s <<"\"" <<endl;

}

}