Regex Guru

Wednesday, 12 August 2009

Updated PCRE DLL for TPerlRegEx

Filed under: Regex Libraries — Jan Goyvaerts @ 16:44

Some users have reported that TPerlRegEx for Delphi 2009 causes their applications to crash when the component is freed. I finally got around to testing this in detail. It turns out the bug only occurs when using the DLL, and that the crash actually occurs in the DLL when pcre_free is called.

I’ve now released a new version of TPerlRegEx that comes with a new DLL that exports a custom pcre_dispose function which the component now calls instead of pcre_free. To upgrade to the new version, simply recompile your application with the new PerlRegEx.pas and pcre.pas, and replace pcre3.dll with pcrelib.dll. The new DLL is compiled from the PCRE 7.9 sources.

While I was at it, I also updated the OBJ files to PCRE 7.9. To use the OBJ files instead of the DLL, edit pcre.pas to comment out the PCRE_LINKDLL define and uncomment the PCRE_STATICLINK define. Due to bugs in the Delphi compiler, this only works if you do not install TPerlRegEx into a design time package. That means you’ll have to instantiate the component at runtime instead of dropping it onto a form.

Download TPerlRegEx. Source is included under the MPL 1.1 license.

Update: I hadn’t properly updated all the files in the .zip file on on August 12th. This has now been fixed.


  1. Hi,

    During the installation of the new TPerlRegEx on Delphi 2009 everything works fine, that is, TPerlRegEx is installed without any error.
    But when Delphi 2009 is closed and then reopened the following error message is showned:

    “bds.exe – Unable To Locate Component
    This application has failed to start because pcrelib.dll was not found. Re-installing the application may fix this problem.”

    To solve this problem I have copied the ‘pcrelib.dll’ file to the following folder:

    C:\Documents and Settings\All Users\Documents\RAD Studio\6.0\Bpl

    Is it the correct way to solve this problem?

    Do I have to copy the ‘pcrelib.dll’ file to the ‘c:\Windows\System32’ folder of the computers where the applications that uses the TPerlRegEx component have been installed?

    Thanks for any help.

    Comment by Marcleo Colonna Rosman — Monday, 17 August 2009 @ 20:42

  2. If you want to install TPerlRegEx into a design time package, then you have to put pcrelib.dll in a folder that’s on your system path. This is no different than installing any Delphi package that depends on a DLL.

    If you compile your application with the DLL, you can put pcrelib.dll in the same folder as your application’s .exe file.

    If you don’t want to use the DLL, edit pcre.pas as indicated by the comments in that file. If you do that, you can’t install the package into the IDE because of internal compiler errors. (Installing the package will work, but compiling your application won’t unless you put TPerlRegEx into a runtime package.)

    Comment by Jan Goyvaerts — Tuesday, 18 August 2009 @ 11:12

  3. Hi,

    Just tried TPerlRegEx and was stumbled upon one problem: pattern “(?)(?=<“. It took some time for me to find PCRE_NOTEMPTY option in PCRE and find that it set to True in TPerlRegEx.Create() by default. Can’t judge consistency of this flag being set by default, but I believe it should be mentioned somewhere. And it should be done in some expressive way to draw additional attention. Just for people who came to PCRE from Perl where there is no such option. Think this is rather important, especially for split and replace functions. BTW, snippets from RegexBuddy that use zero-length match (i.e., insert thousand separators) shouldn’t work either (if there is no special ‘Regex.State := []’ command).

    Thanks for your attention.

    Comment by elricbk — Wednesday, 19 August 2009 @ 1:52

  4. I have installed the ‘TPerlRegEx component with the Updated PCRE DLL’ then I have copied the ‘pcrelib.dll’ file to the ‘C:\Documents and Settings\All Users\Documents\RAD Studio\6.0\Bpl’ folder and to the folder of the .exe file of the application I am developing with Delphi 2009.

    When I trace any statement related to that new version of the TPerlRegEx component on the application’s code the Debug CPU Window is showned and the tracing of the code is transferred to that CPU Window.

    This does not happen with the prior TPerlRegex component (with ‘pcre.dll’ version 7.0.2632.17573).

    What have I done? I have only removed the prior TPerlRegex component and installed the new version of the TPerlRegex component.

    Comment by Marcelo Colonna Rosman — Wednesday, 19 August 2009 @ 3:01

  5. Hi,

    I installed the TPerlRegEx component with the pcre.dll version 7.0.2632.17573 on Delphi 2009.

    (The TPerlRegEx with the updated pcre.dll does not work for me. The DEBUG CPU Window opens when I start to trace the code of the application and I can not close that DEBUG CPU Window. It does not happen with the TPerlRegEx component with the pcre.dll version 7.0.2632.17573).

    To find the position of a matched character in the PerlRegEx.Subject I am using the PerlRegEx.MatchedExpressionOffset property.

    But it gives wrong results if the size of all the characters of the subject string are not equal to 1 byte each character.

    For example:

    PerlRegEx.Subject := ‘No.123456789 10/10/2000 401Tit.*XIKY CONFECÇÕES LTDA.’;
    PerlRegEx.RegEx := ‘(?i)Õ’;
    if PerlRegEx.Match then
    lblPosSubStr.Caption := IntToStr(PerlRegEx.MatchedExpressionOffset);

    // PerlRegEx.MatchedExpressionOffset = 46 instead of 45.

    bytes/char: 11111111111111111111111111111111111111111112211111111
    string: ‘No.123456789 10/10/2000 401Tit.*XIKY CONFECÇÕES LTDA.’
    Position: 12345678901234567890123456789012345678901234567890123

    The ‘Ç’ character ocupies 2 bytes and the ‘Õ’ character ocupies 2 bytes.
    The correct position (MatchedExpressionOffset) of the ‘Õ’ character should be 45 and not 46.

    It seems that the PerlRegEx.MatchedExpressionOffset property returns the position of the ‘Õ’ character based on the number of bytes and not on the number of characters.

    Is there any solution for this problem?

    Comment by Marcelo Colonna Rosman — Friday, 28 August 2009 @ 1:47

  6. PCRE itself can only deal with 8-bit character sets and with UTF-8. When using TPerlRegEx with Delphi 2009, it will use UTF-8. All the string properties of TPerlRegEx use UTF8String, and all offset and length properties use UTF-8 byte counts. MatchedExpressionOffset points to the correct character in the Subject property of TPerlRegEx.

    Comment by Jan Goyvaerts — Friday, 28 August 2009 @ 10:43

  7. Any idea when you will find time to update TPerlRegEx for Delphi 2010?

    Tried to install it in Delphi 2010. It seemed to work. Then opened a Delphi 6 application and received the error that that the bds.exe unable to locate Component. The pcrelib.dll could not be found. Did add the folder for TPerlRegEx to the Library Path.

    Comment by Doug — Thursday, 8 October 2009 @ 0:09

  8. If you compile TPerlRegEx to use the DLL and install it into the IDE, then you need to put the DLL in a folder that is on your Windows system path so Windows can find it when the IDE wants to load the DLL.

    Comment by Jan Goyvaerts — Thursday, 8 October 2009 @ 14:45

Sorry, the comment form is closed at this time.