SF Area/Download
Docs:
User
Internal/Devel
Support This Project
SourceForge.net Logo

CSRegEx User Documentation

Introduction

The purpose of CSRegEx is to be the simplest to use regular expression class with the least amount of restrictions on the license.

For features, see features.
For more detailed examples, see examples.

Requirements

Before you begin, you need STL to use this. To use CSRegEx, simply add the file csregex.cpp in your project. You may also need to set the include path in your compiler so that it can find csregex.h, but most compilers are smart enough to do this automatically.

Supports UNICODE! If UNICODE is enabled in your compiler, or _UNICODE is defined, then all functions will use wchar_t instead of char.

Usage

First, include the header file (place this around the top of your .cpp file)

 #include "csregex.h"

Second, create an instance.

 CSRegEx *re = new CSRegEx();

There are basically 2 functions and 1 other general purpose function.

  • Compile(char *regex); // returns true on OK
  • Match(char *str); // returns true on match.
  • MatchRE(char *str, char *regex); // do both compile and match.

Compile

First, regular expressions must be compiled into an internal format. This is not compiling as in your C++ compiler. It's simply a conversion process. So think of it as a simple conversion routine.

 re->Compile("[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?");

The regular expression example above is for a floating point number. Also, the Compile() function will return false on error. It will return true if everything went ok.

On error (return value of false):

  • re->error will hold the index in the regular expression string where the error occured.
  • re->error_code will tell what went wrong. Id's are in the header file.
  • re->error_str will point to a human readable error string.

Now if you compiled it successfully, you can match strings any number of times without recompiling the regular expression every time.

The compiled version of the regular expression is stored internally. You can get and set it, but see header file for more info.

Match

Now that we have compiled our regular expression, we want to find matches.

 re->Match("bla blae 0.457");

If the above returned false, then no match was found.

On success (return value of true):

  • re->MatchStart will be the index in the search string where the match was found.
  • re->MatchEnd will be the end index (non-inclusive). This means the character at this index should not be used. This value is useful if you want to continue matching.

And that's all there is to it. Oh, don't forget to delete your instance when you're done with it.

 delete re;

MatchRE

To compile and match all at once, use this:

 re->MatchRE("bla blae 0.457", "[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?");

Returns true if a match found. Returns false on error or no match found.

If it returns false, you sometimes need to know if there simply was no match or if there was an error in compiling the regular expression.

re->error_code will be rge_ok if compiling succeded. In this case, there was simply no match found.

But if re->error_code is NOT rge_ok, then an error in compilation occured and re->error, re->error_code and re->error_str will be set just as described above for the Compile() function.


Docs for CSRegEx created on Tue Dec 11 14:35:39 2007 by Doxygen 1.4.3


Webmaster: Cléo Saulnier