#include #include #include #include using namespace boost; using namespace std; regex expression; class MyCharIter { private: const char *s; int bp; public: MyCharIter() { s = 0; bp=0; } MyCharIter(const char *x) { s = x; bp=0; } MyCharIter(const char *x, int p) { s = x; bp=p; } char operator *() const; MyCharIter operator++(int); // postfix X++ MyCharIter & operator++(); // prefix ++X MyCharIter operator--(int); // postfix X-- MyCharIter & operator--(); // prefix --X bool operator==(const MyCharIter &x) const ; bool operator!=(const MyCharIter &x) const ; MyCharIter & operator= (const MyCharIter &x); typedef std::ptrdiff_t difference_type; typedef char value_type; typedef const char* pointer; typedef const char& reference; //typedef std::bidirectional_iterator_tag iterator_category; typedef std::input_iterator_tag iterator_category; friend string string_between(const MyCharIter &start, const MyCharIter &end); }; char MyCharIter::operator *() const { return s[bp]; } MyCharIter MyCharIter::operator++(int){ // postfix X++ MyCharIter foo(this->s, this->bp); ++bp; return foo; } MyCharIter& MyCharIter::operator++(){ // prefix ++X cout << "NT: ++X called bp=" << bp << " c= '" << s[bp] <<"'" << endl; ++bp; return *this; } MyCharIter MyCharIter::operator--(int){ // postfix X-- cout << "Oh dear - we do really need operator postfix -- \n"; return *this; } MyCharIter& MyCharIter::operator--(){ // prefix --X cout << "Oh dear - we do really need operator prefix -- \n"; return *this; } bool MyCharIter::operator==(const MyCharIter &x) const { return this->s == x.s && this->bp == x.bp ; } bool MyCharIter::operator!=(const MyCharIter &x) const { return ! ( *this == x ); } MyCharIter &MyCharIter::operator= (const MyCharIter &x){ this->s = x.s; this->bp = x.bp; return *this; } string string_between(const MyCharIter &start, const MyCharIter &end){ return string(start.s+start.bp, end.s+end.bp); } MyCharIter my_begin(const char *s){ return MyCharIter(s); } MyCharIter my_end(const char *s){ return MyCharIter(s,strlen(s)); } // -------------------------------------------------------------------- // process_ftp: // on success returns the ftp response code, and fills // msg with the ftp response message. int find_match(MyCharIter &start, MyCharIter end, std::string* msg) { boost::match_results what; #ifdef BOOST_REGEX_V3 boost::regex::flag_type flags = #else boost::regex_constants::match_flag_type flags = #endif boost::match_default | boost::match_not_dot_newline | boost::match_continuous ; if(regex_search(start,end, what, expression, flags) ){ if(msg) msg->assign(what[0].first, what[0].second); start = what[0].second; int i; for(int i=1; ierase(); return -1; } int main(){ // This doesnt seek to the end // boost::regex_constants::syntax_option_type sflag = regex_constants::normal; // But this does boost::regex_constants::syntax_option_type sflag = boost::regex::extended; expression.assign("(aaa)|(bbb)|(.)|(\n)", sflag); const char * input = "aaa bbb ccc\naaaaaaX"; MyCharIter start = my_begin(input), end = my_end(input); string res; int i; while( ( i = find_match(start,end,&res) ) != -1 ){ cout << "******** Match = '" << res << "' regex case = " << i << endl; } }