Difference between revisions of "RegEx packages"

From Lazarus wiki
Jump to navigationJump to search
Line 6: Line 6:
  
 
This is the most complete implementation, available in packages/regexpr/src/regexpr.pas
 
This is the most complete implementation, available in packages/regexpr/src/regexpr.pas
 +
 +
Read the wikipedia page about POSIX regular expressions to learn the syntax: http://en.wikipedia.org/wiki/Regular_expression#POSIX_Basic_Regular_Expressions
 +
 +
===Simple usage example===
 +
 +
Using Sorokin RegExpr to check if an expression is present in a string is very easy, just create an instance of TRegExpr, then place your regular expression in the property TRegExpr.Expression and use the method Exec to verify if there are any matches for this regular expression. Exec will return true if the expression matches the string passed to it.
 +
 +
<delphi>
 +
var
 +
  RegexObj: TRegExpr;
 +
begin
 +
  RegexObj := TRegExpr.Create;
 +
  RegexObj.Expression := '.*login.*';
 +
  if RegexObj.Exec('Please try to login here') then WriteLn('The login was found!');
 +
  RegexObj.Free;
 +
end;
 +
</delphi>
  
 
==Regexpr by Florian==
 
==Regexpr by Florian==

Revision as of 07:24, 6 September 2011

Template:Regexpr

The RegExpr package includes a number of regular expressions implementations

Regexpr by Sorokin

This is the most complete implementation, available in packages/regexpr/src/regexpr.pas

Read the wikipedia page about POSIX regular expressions to learn the syntax: http://en.wikipedia.org/wiki/Regular_expression#POSIX_Basic_Regular_Expressions

Simple usage example

Using Sorokin RegExpr to check if an expression is present in a string is very easy, just create an instance of TRegExpr, then place your regular expression in the property TRegExpr.Expression and use the method Exec to verify if there are any matches for this regular expression. Exec will return true if the expression matches the string passed to it.

<delphi> var

 RegexObj: TRegExpr;

begin

 RegexObj := TRegExpr.Create;
 RegexObj.Expression := '.*login.*';
 if RegexObj.Exec('Please try to login here') then WriteLn('The login was found!');
 RegexObj.Free;

end; </delphi>

Regexpr by Florian

This is the olded. It is present in packages/regexpr/src/old and is not currently compiled by the makefiles

Regexpr by Joost

Regexpr by Joost (units oldregexpr.pp and regex.pp) is a very basic regex (Regular Expression) unit, it handles most regular expressions as GNU regexpr. The use of regex is to search patterns inside a string.

The current unit is far from complete, and still misses very simple syntax support of POSIX or more complex syntax such as Perl regex, Java Regex, Ruby Regex etc...

The unit contains 4 functions for now:

  • GenerateRegExprEngine – This function compiles the regex pattern.
  • RegExprPos – Finds the pattern inside a given string.
  • DestroyRegExprEngine – Free the compilation of the pattern
  • RegExprEscapeStr – Escape reserve syntax of regular expression language so it will be understood as string instead of regex syntax.

There is also one test:

  • The testreg1 test program demonstrates the supported regular expressions.

Go to back Packages List