/** @file
$ Id $
$ Date $
$ Author $
@ copy & copy ; Marc W & auml ; ckerlin
@ license LGPL , see file < a href = " license.html " > COPYING < / a >
$ Log $
Revision 1.2 2004 / 12 / 16 13 : 09 : 31 marc
possibility to evaluate and extract sub expressions
Revision 1.1 2004 / 12 / 14 20 : 20 : 30 marc
initial version
*/
# include <mrw/string.hpp>
# include <mrw/regexp.hpp>
# include <mrw/exception.hpp>
namespace mrw {
RegExp : : RegExp ( const std : : string & pattern , bool hassub , int flags )
throw ( std : : exception ) :
_hassub ( hassub ) {
if ( flags & nosub ) throw mrw : : invalid_argument ( " nosub " ) ;
if ( regcomp ( & _regex , pattern . c_str ( ) , ( _hassub ? flags : ( flags | nosub ) ) ) )
throw mrw : : invalid_argument ( pattern ) ;
}
RegExp : : ~ RegExp ( ) throw ( ) {
regfree ( & _regex ) ;
}
bool RegExp : : operator ( ) ( const std : : string & text ) throw ( std : : bad_exception ) {
if ( _hassub )
return ! regexec ( & _regex , ( _text = text ) . c_str ( ) , MAX_SUB , _sub , 0 ) ;
else
return ! regexec ( & _regex , text . c_str ( ) , 0 , 0 , 0 ) ;
}
std : : string RegExp : : operator [ ] ( unsigned int n ) const throw ( std : : exception ) {
if ( ! _hassub )
throw mrw : : invalid_argument ( " initialized with no sub expressions " ) ;
if ( n > = MAX_SUB | | _sub [ n ] . rm_so < 0 | | _sub [ n ] . rm_eo < 0 )
throw mrw : : invalid_argument ( mrw : : string ( n ) ) ;
return _text . substr ( _sub [ n ] . rm_so , _sub [ n ] . rm_eo - _sub [ n ] . rm_so ) ;
}
}