TPCREWrapper class is a wrapper around well known PCRE library. PCRE stands for “Perl Compatible Regular Expressions”. As stated on http://www.pcre.org , PCRE library is a set of functions that implements regular expression pattern matching using the same syntax and semantics as Perl 5.
PCRE is a powerful library. As far as I know, is one of the few regexp libraries which implements partial matching, allowing user to match a regular expression against a “future” string. Commonly, regular expressions are used to find (match) a pattern in a subject string, which means the subject string is entirely available at the moment of matching. But if the subject string comes from a stream, it may become available gradually. In such a situation, it may be useful do distinguish between a “no match” situation and “match so far but not enough characters for a full match”, i.e. a partial matching.
Unfortunately for Delphi users, PCRE library is written in C which makes it tricky to integrate into a Delphi project. The library also exhibits a “procedural” approach and not an object oriented approach. This is why I considered useful to implement a wrapper around PCRE.
Compiling PCRE library may be a tricky process too. PCRE cannot be built using gnu system build. To be more accurate, the resulting library is not usable by Delphi. And I wanted to avoid using PCRE as a dynamic link library. So, I used Borland free compiler to compile PCRE in a Delphi usable form.
If you want to rebuild yourself the library, there’s how it’s done:
1. Download Borland free C++ compiler. You can get it from the following link:
Of course, if you have any version of CBuilder from Borland or Embarcadero, the free compiler won’t be necessary.
The pcre library itself must reside in PCREWrapper\pcre folder. Download the library from the following link:
After downloading the library unzip it in the above folder and run makelibrary.cmd. You may want to tweak my Makefile to better tailor your needs, especially if your environment differs. A few words of warning though: Don’t change the calling convention paradigm. If you change it from -pc (cdecl), the objects won’t link against pcre.dcu. Also, if you use the free compiler, do not set HAVE_STDINT_H or HAVE_INTTYPES_H to 1 because borland free compiler does not have any of stdint.h or inttypes.h headers.
Once the library is compiled, the resulting object files can be linked and used in a Delphi standard dcu.
Now, a little background on TPCREWrapper class:
Of course, I’m not the inventor of the wheel here! Others have implemented PCRE wrappers before. One notable implementation is JVCL library on http://jvcl.delphi-jedi.org . There’s also around some very good native delphi regexp libraries, written in pure pascal like TRegexp for instance. So, why another wrapper?
First of all, JVCL is HUGE, whith hundreds of classes. And I only needed a REGEXP parser. Most important, JVCL wrapper does not implement partial matching, nor TRegExp library and I needed that facility. So, that’s how those classes comes to exist.
Below, some comments regarding TPCREWrapper class implementation:
1 .The wrapper itself is a descendant of TObjectList in order to easily maintain it’s subexpressions list. Subexpressions themselves are implemented as descendants of TObject. In a real application, subexpressions must be of TSubExpression class or a descendant of TSubExpression class.
2. The match method is a procedure! This may seem strange. No result of matching? No error code? Well…yes, but… If you read PCRE documentation carefully, you’ll see that the matching function pcre_exec returns the matching position in subject if a match was found or an error code if no matching was found. In an object oriented approach though, this is not very useful because the wrapper class itself takes care of extracting subexpressions, so we don’t need starting position of matched string as a result.
So, when no mach is found, an exception is raised instead. There are three exception types, all descending from EPCREError
EPCRECompileError = class (EPCREError); // raised on regexp compile error
EPCREMatchError = class (EPCREError); // raised if any matching error occurs (pattern not found, partial match etc)
EPCREStudyError = class (Exception); // raise if there is any study error.
There is also included a short test program for TRegExpression class which should be self explanatory.
You can download all library files (including a short test project) from PCRELibrary
That’s all. I hope that you will find this informatiion useful and also will put TPCREWrapper class to good use
fake cheap louboutins buy replica louboutins online fake cheap shoes