clear solution for fuzzy tasks
EXECryptor
strongest anti-crack & anti-piracy software protection and license management system with custom security level settings and ultra short serial number generation
Pattern String Engine
for Delphi

Pattern String Engine (PSE) is a freeware library for work with regular expressions in Borland Delphi/Kylxi. As distinct from the other libaries, the regular expressions are described by special pattern collection and not by symbol line. The library contains the great number of intrinsiced patterns and allows to easy create the new patterns for specific applications.

PSE allows not just checking the line to similarity with regular expression, but also getting all possible ways of similarity the line to the pattern. Some parts of line can be extracted from to variable for the next processing.


The base abstract class in Pattern String Engine is TPattern.

TPattern = class(TObject)
public
    constructor Create(const APattern: array of const);
    procedure Activate(const S: string);
    function Match: Boolean;
    procedure MatchNext; virtual; abstract;
end;

The various particularized patterns are generated from it:

  • TPSE - pattern list
  • TPSEOneOf - any pattern of list
  • TPSEMayBe - optional pattern
  • TPSECharSet - symbol line from asked charset
  • TPSEVar - pattern of compliance of line variable contents
  • TPSESetLen - pattern constrained to min/max length

The main idea is regular expressions may be represented as simple patterns tree. For example, identifier pattern in programming language - letters and numbers order started with letter:

[a-z,A-Z] { [a-z,A-Z,0-9] }

describes in PSE in such way:

PSE( [ PSEChar('a..zA..Z'), PSECharSet('a..zA..Z0..9'#0) ] )

PSE, PSEChar and PSECharSet - are the functions that create object-pattern of conformable tipes.

The example of more complicated pattern that processes dates at various sizes:

PSE( [
    PSE( [ PSEDigit, PSEDigit ]),
    PSEChar('/. ')[sep],
    PSEOneOf( [
        PSE( [ PSEDigit, PSEDigit ] ),
        PSEOneOf( [ 'jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec' ] )
    ] ),
    PSEVar(sep), // must be same separator as between day and month
    // 2 or 4 digits for year
    PSE( [ PSEDigit, PSEDigit, PSEMayBe([PSEDigit,PSEDigit ] ) ] )
] );

This pattern identifies the differnt month spelling (by numbers and abbriviation), looks after separators to be similar.

Having a created pattern we can check out the similarity of any line to this pattern.For example, the next code checks out the similarity SourceString line to the pattern <number> [+ -* /] <number>

var pat: TPattern;
..............................
// Create pattern
pat: = PSE( [PSENumber, PSEChar('+-*/'), PSENumber] );
// Activate pattern on some string
pat.Activate (SourceString);
if pat.Match then begin
   // Match OK
end;
// Free pattern

pat.Free;

Usually it is necessary to define the compliance of line to the pattern, and to divide the line into some elemts. It` s very easy to do using PSE:

var pat: TPattern;
    num1, num2, sign: string;
..............................
// Create pattern
pat: = PSE( [PSENumber[num1],
             PSEChar('+-*/')[sign],
             PSENumber[num2]] );
// Activate pattern on some string
pat.Activate(SourceString);
if pat.Match then // Math OK
  case sign[1] of
    '+': Result:=StrToFloat(num1) + StrToFloat(num2);
    '-': Result:=StrToFloat(num1) - StrToFloat(num2);
    '*': Result:=StrToFloat(num1) * StrToFloat(num2);
    '/': Result:=StrToFloat(num1) / StrToFloat(num2);
  end
else
  
Result:=0;
// Free pattern
pat.Free;

In comparison process, the resalts will be saved to variables num1, num2 and sign.

Download Pattern String Engine for Delphi with source code ...