Board index » delphi » Searching for string matching code

Searching for string matching code

Does anybody know about any source or component that deals with string
matching (wildcarding, regular expressions, etc)?

Thanks
Hans-Peter
--
Hans-Peter Haberlandner     |
Technodat CAD/CAM           | email: h...@technodat.co.at
Jakob Haringer-Strasse 6    | Phone: +43 - (0)662 454944 670
A-5020 Salzburg             | FAX:   +43 - (0)662 454944 9

 

Re:Searching for string matching code


Quote
Hans-Peter Haberlandner wrote:

> Does anybody know about any source or component that deals with string
> matching (wildcarding, regular expressions, etc)?

> Thanks
> Hans-Peter
> --
> Hans-Peter Haberlandner     |
> Technodat CAD/CAM           | email: h...@technodat.co.at
> Jakob Haringer-Strasse 6    | Phone: +43 - (0)662 454944 670
> A-5020 Salzburg             | FAX:   +43 - (0)662 454944 9

Have you ever tryed to work with the POS function ?

Re:Searching for string matching code


Quote
> Does anybody know about any source or component that deals with string
> matching (wildcarding, regular expressions, etc)?

This source (for Delphi, sorry - converting is probably easy). Use:

SameFile(file to look for, wildcards, case sensitive on/off).

Returns true if File1 matches the wildcard File2.

Wildcards are * and ?.

Unit filespec;

interface

uses
  SysUtils;

type
  TWildCard = class
    public
      Function SameFile(File1, File2 : string; isCase : boolean) : Boolean;
    private
      Function SameName(N1, N2 : string) : Boolean;
  end;

implementation

Function TWildCard.SameName(N1,N2:string) : Boolean;
Var
  P1,P2 : Byte;
  Match : Boolean;
Begin
  P1    := 1;
  P2    := 1;
  Match := True;

  If (Length(N1) = 0) And (Length(N2) = 0) Then
    Match := True
  Else
    If Length(N1) = 0 Then
      If N2[1] = '*' Then
        Match := True
      Else
        Match := False
    Else
      If Length(N2) = 0 Then
        If N1[1] = '*' Then
          Match := True
        Else
          Match := False;

  While (Match = True) And (P1 <= Length(N1)) And (P2 <= Length(N2)) Do
    If (N1[P1] = '?') Or (N2[P2] = '?') Then
    Begin
      Inc(P1);
      Inc(P2);
    End
    Else
      If N1[P1] = '*' Then
      Begin
        Inc(P1);
        If P1 <= Length(N1) Then
        Begin
          While (P2 <= Length(N2)) And Not
SameName(Copy(N1,P1,Length(N1)-P1+1),Copy(N2,P2,Length(N2)-P2+1)) Do
            Inc(P2);
          If P2 > Length(N2) Then
            Match := False
          Else
          Begin
            P1 := Succ(Length(N1));
            P2 := Succ(Length(N2));
          End;
        End
        Else
          P2 := Succ(Length(N2));
      End
      Else
        If N2[P2] = '*' Then
        Begin
          Inc(P2);
          If P2 <= Length(N2) Then
          Begin
            While (P1 <= Length(N1)) And Not
SameName(Copy(N1,P1,Length(N1)-P1+1),Copy(N2,P2,Length(N2)-P2+1)) Do
              Inc(P1);
            If P1 > Length(N1) Then
              Match := False
            Else
            Begin
              P1 := Succ(Length(N1));
              P2 := Succ(Length(N2));
            End;
          End
          Else
            P1 := Succ(Length(N1));
        End
        Else
          If N1[P1] = N2[P2] Then
          Begin
            Inc(P1);
            Inc(P2);
          End
          Else
            Match := False;
        If P1 > Length(N1) Then
        Begin
          While (P2 <= Length(N2)) And (N2[P2] = '*') Do
            Inc(P2);
          If P2 <= Length(N2) Then
           Match := FALSE;
        End;
        If P2 > Length(N2) Then
        Begin
          While (P1 <= Length(N1)) And (N1[P1] = '*') Do
            Inc(P1);
          If P1 <= Length(N1) Then
            Match := False;
        End;
        SameName := Match;
End;

{
----------------------------------------------------------------------------

Quote
}

Function TWildCard.SameFile(File1,File2 : string; isCase : boolean) :
Boolean;
Begin
  if not isCase then
  begin
    File1 := Uppercase(File1);
    File2 := Uppercase(File2);
  end;
  SameFile := SameName(File1,File2);
End;

End.

--
Brian Pedersen, System Specialist, Alta Copenhagen
url: http://www.alta.dk
personal: http://home6.inet.tele.dk/brianp

Remove NOSPAM to reply.

Other Threads