Recursive Comparison - string & mask

Share your advanced PureBasic knowledge/code with the community.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Recursive Comparison - string & mask

Post by einander »

;Compare string with mask - Recursive search
;by Einander - Registered PB user
;October 8 - 2003 - PB 3.80
;

Code: Select all

Procedure.s FindMask(Orig$,Msk$)
    Shared Compare$
    in=FindString(Msk$,"*",1)
    If in
        LMask$=Left(Msk$,in-1)
        RMask$= Mid(Msk$,in+1,Len(Msk$))
        in=FindString(Orig$,LMask$,1)
        If in
            Compare$=Compare$+"*"+LMask$
            FindMask(Mid(Orig$,in+2,Len(Orig$)),rMask$) 
        EndIf
    Else
       Compare$=Compare$+"*"+Msk$
    EndIf
   ProcedureReturn Compare$
EndProcedure
;_____________________________________________________________
;
; Use * on mask to replace any text
Original$="This is the original text with included mask fragments"
Mask$="**is*nal*ext wit***uded*ment"
While Left(Mask$,1)="*":Mask$=Mid(Mask$,2,Len(Mask$)):Wend ; cleans mask
While Right(Mask$,1)="*":Mask$=Left(Mask$,Len(Mask$)-1):Wend
If Mask$=Original$
    Debug "1 Mask equals Original"
ElseIf Len(Mask$)>Len(Original$)
    Debug "0 Mask too long"
Else
    Found$=FindMask(Original$,Mask$):Found$=Mid(Found$,2,Len(Found$))
    If Found$=Mask$ 
        Debug "1 Found : "+Found$ 
    Else
        Debug "0 Not found!"
    EndIf 
EndIf
 
User avatar
zxtunes.com
Enthusiast
Enthusiast
Posts: 375
Joined: Wed Apr 23, 2008 7:51 am
Location: Saint-Petersburg, Russia
Contact:

Re: Recursive Comparison - string & mask

Post by zxtunes.com »

This work not true. :cry:

Test this:

Code: Select all

Original$="m.proc1 .CP "
Mask$="*.W" 
result: "1 Found .W" = false
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

You can try this
Updated for PB 4.xx - Non Recursive

Code: Select all

Procedure$ FindMask(Orig$,Mask$)
   While Left(Mask$,1)="*":Mask$=Mid(Mask$,2,Len(Mask$)):Wend ; cleans mask
   While Right(Mask$,1)="*":Mask$=Left(Mask$,Len(Mask$)-1):Wend
   While FindString(Mask$,"**",1):  Mask$=ReplaceString(Mask$,"**","*"): Wend
   
   If Len(Mask$)>Len(Orig$)
      MessageRequester("",Orig$+Chr(10)+Mask$+Chr(10)+"Mask too Long",1)
      ProcedureReturn ""
   EndIf
   
   Repeat
      i+1
      A$=StringField(Mask$,i,"*")
      If A$="":Break:EndIf
      If FindString(Orig$,A$,1)
         Result$+A$+" "
      Else
         ProcedureReturn ""
      EndIf
   ForEver
   ProcedureReturn Trim(Result$)
EndProcedure
   
   ;__________________________________________________
   ;
   ; Use * on mask to replace any text
Original$="This is the original text with included mask fragments"
Mask$="**is*nal*ext wit***uded*ment* *"
   
Found$=FindMask(Original$,Mask$)
If Len(Found$)
   MessageRequester("","Found "+Chr(10)+Found$,1)
Else
   MessageRequester("","Not found!",1)
EndIf
Post Reply