Main Page | Class Hierarchy | Class List | File List | Class Members

simpleregex.h

00001 //
00002 // regex.hpp 1.0 Copyright (c) 2003 Peter Petersen (pp@on-time.de)
00003 // Simple C++ wrapper for PCRE
00004 //
00005 // This source file is freeware. You may use it for any purpose without
00006 // restriction except that the copyright notice as the top of this file as
00007 // well as this paragraph may not be removed or altered.
00008 //
00009 // This header file declares class RegEx, a simple and small API wrapper
00010 // for PCRE.
00011 //
00012 // RegEx::RegEx(const char * regex, int options = 0)
00013 //
00014 //    The constructor's first parameter is the regular expression the
00015 //    created object shall implement. Optional parameter options can be
00016 //    any combination of PCRE options accepted by pcre_compile(). If
00017 //    compiling the regular expression fails, an error message string is
00018 //    thrown as an exception.
00019 //
00020 // RegEx::~RegEx()
00021 //
00022 //    The destructor frees all resources held by the RegEx object.
00023 //
00024 // int RegEx::SubStrings(void) const
00025 //
00026 //    Method SubStrings() returns the number of substrings defined by
00027 //    the regular expression. The match of the entire expression is also
00028 //    considered a substring, so the return value will always be >= 1.
00029 //
00030 // bool RegEx::Search(const char * subject, int len = -1, int options = 0)
00031 //
00032 //    Method Search() applies the regular expression to parameter subject.
00033 //    Optional parameter len can be used to pass the subject's length to
00034 //    Search(). If not specified (or less than 0), strlen() is used
00035 //    internally to determine the length. Parameter options can contain
00036 //    any combination of options PCRE_ANCHORED, PCRE_NOTBOL, PCRE_NOTEOL.
00037 //    PCRE_NOTEMPTY. Search() returns true if a match is found.
00038 //
00039 // bool RegEx::SearchAgain(int options = 0)
00040 //
00041 //    SearchAgain() again applies the regular expression to parameter
00042 //    subject last passed to a successful call of Search(). It returns
00043 //    true if a further match is found. Subsequent calls to SearchAgain()
00044 //    will find all matches in subject. Example:
00045 //
00046 //       if (Pattern.Search(astring)) {
00047 //          do {
00048 //             printf("%s\n", Pattern.Match());
00049 //          } while (Pattern.SearchAgain());
00050 //       }
00051 //
00052 //    Parameter options is interpreted as for method Search().
00053 //
00054 // const char * RegEx::Match(int i = 1)
00055 //
00056 //    Method Match() returns a pointer to the matched substring specified
00057 //    with parameter i. Match() may only be called after a successful
00058 //    call to Search() or SearchAgain() and applies to that last
00059 //    Search()/SearchAgain() call. Parameter i must be less than
00060 //    SubStrings(). Match(-1) returns the last searched subject.
00061 //    Match(0) returns the match of the complete regular expression.
00062 //    Match(1) returns $1, etc.
00063 //
00064 // The bottom of this file contains an example using class RegEx. It's
00065 // the simplest version of grep I could come with. You can compile it by
00066 // defining REGEX_DEMO on the compiler command line.
00067 //
00068 
00069 #ifndef _SIMPLEREGEX_H
00070 #define _SIMPLEREGEX_H
00071 
00072 #include <string.h>
00073 #include "pcre.h"
00074 
00078 class RegEx
00079 {
00080 public:
00081 
00086   RegEx(const char * regex, int options = 0);
00087 
00089   ~RegEx();
00090 
00094   inline int RegEx::SubStrings(void) const
00095     {
00096       return substrcount;
00097     }
00098 
00105   bool Search(const char * subject, int len = -1, int options = 0);
00106 
00120   bool SearchAgain(int options = 0);
00121   
00126   const char * Match(int i);
00127 
00128 private:
00129   inline void ClearMatchList(void);
00130   pcre * re;
00131   pcre_extra * pe;
00132   int substrcount;
00133   int * ovector;
00134   const char * lastsubject;
00135   int slen;
00136   const char * * matchlist;
00137 };
00138 
00139 #endif // _SIMPLEREGEX_H

Generated on Tue Sep 11 12:07:44 2007 for libIXAml by doxygen 1.3.5