?
Solved

String Match

Posted on 2003-03-20
4
Medium Priority
?
456 Views
Last Modified: 2010-04-03
Anyone know any free component or algorith or code about how to string match with WILDCARD in Delphi?

I want to test this

   if (match("just a test", "*a*test*")) then
   begin
     { yes, it is match }
   end;

Thanks.
0
Comment
Question by:klompen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8173727
go to
www.madshi.net

and get the madStrings

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8173735
sorry,

madStrings are included in madBasics
-> get madBasics
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8173825
uses masks;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if MatchesMask(Edit1.Text, edit2.Text) then
    Caption := 'yes' else Caption := 'not';
end;

wbr, mo.
0
 
LVL 6

Accepted Solution

by:
DaFox earned 80 total points
ID: 8174246
Hi klompen.

I heard that MatchesMask doesn't work reliable. I must confess that I never had problems though. However, this code works perfect! I once found it on the web...

Markus

-

{*****************************************************************}
{* This function implements a subset of regular expression based *}
{* search and is based on the translation of PattenMatch() API   *}
{* of common.c in MSDN Samples\VC98\sdk\sdktools\tlist           *}
{*****************************************************************}
{* MetaChars are  :                                              *}
{*            '*' : Zero or more chars.                          *}
{*            '?' : Any one char.                                *}
{*         [adgj] : Individual chars (inclusion).                *}
{*        [^adgj] : Individual chars (exclusion).                *}
{*          [a-d] : Range (inclusion).                           *}
{*         [^a-d] : Range (exclusion).                           *}
{*       [a-dg-j] : Multiple ranges (inclusion).                 *}
{*      [^a-dg-j] : Multiple ranges (exclusion).                 *}
{*  [ad-fhjnv-xz] : Mix of range & individual chars (inclusion). *}
{* [^ad-fhjnv-xz] : Mix of range & individual chars (exclusion). *}
{*****************************************************************}

unit MatchPtn;

interface

function MatchPattern(InpStr,Pattern :PChar) :Boolean;

implementation

function MatchPattern(InpStr,Pattern :PChar) :Boolean;
begin
  result := true;
  while (True) do begin
    case Pattern[0] of
      #0 :begin
            //End of pattern reached.
            Result := (InpStr[0] = #0); //TRUE if end of InpStr.
            Exit;
          end;

      '*':begin //Match zero or more occurances of any char.
            if (Pattern[1] = #0) then begin
              //Match any number of trailing chars.
              Result := True;
              Exit;
            end
            else
              Inc(Pattern);

            while (InpStr[0] <> #0) do begin
              //Try to match any substring of InpStr.
              if (MatchPattern(InpStr,Pattern)) then begin
                Result := True;
                Exit;
              end;

              //Continue testing next char...
              Inc(InpStr);
            end;
          end;

      '?':begin //Match any one char.
            if (InpStr[0] = #0) then begin
              Result := False;
              Exit;
            end;

            //Continue testing next char...
            Inc(InpStr);
            Inc(Pattern);
          end;

      '[':begin //Match given set of chars.
            if (Pattern[1] in [#0,'[',']']) then begin
              //Invalid Set - So no match.
              Result := False;
              Exit;
            end;

            if (Pattern[1] = '^') then begin
              //Match for exclusion of given set...
              Inc(Pattern,2);
              Result := True;
              while (Pattern[0] <> ']') do begin
                if (Pattern[1] = '-') then begin
                  //Match char exclusion range.
                  if (InpStr[0] >= Pattern[0]) and (InpStr[0] <= Pattern[2]) then begin
                    //Given char failed set exclusion range.
                    Result := False;
                    Break;
                  end
                  else
                    Inc(Pattern,3);
                end
                else begin
                  //Match individual char exclusion.
                  if (InpStr[0] = Pattern[0]) then begin
                    //Given char failed set element exclusion.
                    Result := False;
                    Break;
                  end
                  else
                    Inc(Pattern);
                end;
              end;
            end
            else begin
              //Match for inclusion of given set...
              Inc(Pattern);
              Result := False;
              while (Pattern[0] <> ']') do begin
                if (Pattern[1] = '-') then begin
                  //Match char inclusion range.
                  if (InpStr[0] >= Pattern[0])and(InpStr[0] <= Pattern[2]) then begin
                    //Given char matched set range inclusion.
                    // Continue testing...
                    Result := True;
                    Break;
                  end
                  else
                    Inc(Pattern,3);
                end
                else begin
                  //Match individual char inclusion.
                  if (InpStr[0] = Pattern[0]) then begin
                    //Given char matched set element inclusion.
                    // Continue testing...
                    Result := True;
                    Break;
                  end
                  else
                    Inc(Pattern);
                end;
              end;
            end;

            if (Result) then begin
              //Match was found. Continue further.
              Inc(InpStr);
              //Position Pattern to char after "]"
              while (Pattern[0] <> ']')and(Pattern[0] <> #0) do
                Inc(Pattern);
              if (Pattern[0] = #0) then begin
                //Invalid Pattern - missing "]"
                Result := False;
                Exit;
              end
              else
                Inc(Pattern);
            end
            else
              Exit;
          end;

     else begin //Match given single char.
            if (InpStr[0] <> Pattern[0]) then begin
              Result := False;
              Break;
            end;

            //Continue testing next char...
            Inc(InpStr);
            Inc(Pattern);
          end;
    end;
  end;
end;

end.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question