JCL Help:JclPCRE Using

From Project JEDI Wiki
Jump to navigationJump to search


Summary

Using the JCL PCRE Classes.


Description

The JCL PCRE classes provide access to functions and structures in pcre.dll through the use of TJclRegEx and TJclRegExOptions classes.
TJclRegEx is a class that provides properties and methods that act as wrappers for routines and structures found in the pcre.dll dynamic link library.
TJclRegEx is a non-visual object; it cannot be used on the design surfaces of an IDE. It does not, however, require any installation other that being availble in the search path for the compiler. Simply add the pas unit to the "uses" clause in your application.
uses

   Classes, Windows, SysUtils, Forms, Dialogs,
ActnList, ComCtrls, StdCtrls, Controls,
JclPCRE;

In your program, you would normally allocate an instance of TJclRegEx and configure its' options in the TJclRegEx.Options property.
var

   RE: TJclRegEx;
REO: TJclRegExOptions;
...


 RE := TJclRegEx.Create;
REO := [roIgnoreCase, roMultiLine, roUnGreedy];
RE.Options := REO;

TJclRegEx.Options is used primarily to control the behavior of the regular expression engine during calls to the TJclRegEx.Compile and TJclRegEx.Match methods. Values in the JclRegExOptions set reflect the attributes passed or retrieved using routines in pcre.dll. See JclRegExOptions and JclRegExOption for a detailed description of values.

Use the TJclRegEx.Compile method to specify the Perl-compatible regular expression to use when matching a text subject. TJclRegEx.Compile is essentially a wrapper around the pcre_compile(), pcre_compile2(), pcre_study(), and pcre_maketables() functions in pcre.dll.
See the documentation for the TJclRegEx.Compile method for more details about arguments to the method.
Perl-compatible Regular Expressions are very flexible and very powerful. With all of that utility comes some complexity. Please refer to the pcrepattern documentation for a detailed description of the syntax and semantics of Perl-compatible Regular Expressions.
Use the TJclRegEx.ErrorMessage and TJclRegEx.ErrorOffset properties to examine errors detected when compiling the regular expression.
Use the TJclRegEx.Match method to compare the compiled regular expression against a given subject string using a matching algorithm that is similar to Perl's. The TJclRegEx.Match method is a wrapper around the pcre_exec() function in pcre.dll.
TJclRegEx.Match returns a boolean value to indicate that elements of the regular expression exist in the subject text. Use the TJclRegEx.CaptureCount property to find the number of matching strings found for the regular expression. Use the TJclRegEx.Captures property to access the string values by their ordinal position. Use the TJclRegEx.CaptureRanges property to access the offsets into the subject text where the string match was located.
// look for HTML anchor with HREF attribute

 RE.Compile('<a\s+href\s*=\s*(["'])?(.*)(["'])?(.*)>\s*(.*)\s*<\/a>',
False, False);


 if not RE.Match(memoHTML.Lines.Text) then
begin
MessageDlg('No matches found', mtInformation, [mbOK], 0);
end
else
begin
ShowMessage('Found: ' +
Copy(memoHTML.Lines.Text, RE.CaptureRanges[0].FirstPos,
RE.CaptureRanges[0].LastPos - RE.CaptureRanges[0].FirstPos + 1));
end;

TJclRegEx may raise exceptions when using its properties and methods. These exceptions can normally be handled in your application code by responding to EPCREError exception instances.

See Also

TJclRegEx TJclRegExOptions TJclRegExOption EPCREError pcrepattern pcre_compile pcre_exec


About

Contribute to this help topic

This documentation wiki is based on the collaborative effort of Project JEDI users. Your edits are welcome in order to improve documentation quality: edit this page