Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How do I carry out pattern matching
Message
De
13/04/1999 17:00:51
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00207620
Message ID:
00208007
Vues:
25
John,

I could not sleep last night and your problem kept nagging at me.
So here is a solution (that you can improve upon I am sure).

The following function will look anywhere in a string for a pattern
of numbers and alphas (*#* #*# for a canadian postal code) and
return the portion of the string that match your pattern or the
empty string otherwise.

Function FindPattern
lparameter cPattern,cString

local iLen,iPos,cSubStr,i,lMatchFound

iLen=len(cPattern)
iPos=1
lMatchFound=.f.

do while iPos+iLen-1<=len(cString)
cSubStr=substr(cString,iPos,iLen)
lMatchFound=.t.
i=1
do while i<=iLen
do case
case substr(cPattern,i,1)='#'
if not isdigit(substr(cSubStr,i,1))
lMatchFound=.f.
exit
endif
i=i+1
case substr(cPattern,i,1)='*'
if not isalpha(substr(cSubStr,i,1))
lMatchFound=.f.
exit
endif
i=i+1
otherwise
i=i+1
endcase
enddo
if lMatchFound
return cSubStr
endif
iPos=iPos+1
enddo
return ''

Hope this helps.
This was a fun one to do.

:)

Pascal
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform