Easy pattern matching function. Example:

--------
Pattern = "% is % and %"
TestString = "John is a man and he is single"
Function returns: "TRUE"
String1 = "John"
String2 = "a man"
String3 = "he is single"
---------

Usage:
local str1[100]:byte,str2[100]:byte,str3[100]:byte
invoke MatchPattern,CTEXT("John is a man and he is single"),CTEXT("% is % and %"),addr str1,addr str2,addr str3
Notes:
- Unlimited number of output strings. Add an address of 0 to mark explicit end.
- Case sensitive

Marking an explicit end:
invoke MatchPattern,.........,addr str3,0

	

MatchPattern proc C uses ebx ecx edx esi edi StringInput,FormatString,str1:VARARG
local phase
;----[ prepare stuff ]----------\
mov phase,0
mov edi,str1
mov esi,StringInput
mov ebx,FormatString
;-------------------------------/


_again:
xor edx,edx
.while byte ptr[ebx+edx]!=0 && byte ptr[ebx+edx]!="%"
inc edx
.endw
or edx,edx
jz @F
invoke uArrayCmp,ebx,esi,edx
or eax,eax
jnz _ret
add ebx,edx
add esi,edx
@@:
.if byte ptr[ebx]==0
cmp byte ptr[esi],0
je _good
jmp _ret
.endif

; now I have "%" at [ebx]
inc ebx
xor edx,edx
.while byte ptr[ebx+edx]!=0 && byte ptr[ebx+edx]!="%"
inc edx
.endw
.if !edx
.if edi
.while byte ptr[esi]
mov al,[esi]
mov [edi],al
inc esi
inc edi
.endw
mov byte ptr[edi],0
.endif
jmp _good
.endif
invoke uArrayFind,esi,ebx,edx
or eax,eax
jz _ret
dec eax
.if edi
.while eax
mov cl,[esi]
mov [edi],cl
dec eax
inc esi
inc edi
.endw
mov byte ptr[edi],0
.else
add esi,eax
.endif
.if edi
inc phase
mov edi,phase
mov edi,str1[edi*4]
.endif
jmp _again


_good: mov eax,1
ret
_ret: xor eax,eax
ret
MatchPattern endp

The helper funcs:


uArrayCmp proc uses ebx ecx edx str1,str2,LenI
mov ecx,str1
mov edx,str2
xor eax,eax
cmp LenI,0
je _ret
_again:
mov al,[ecx]
mov bl,[edx]
inc ecx
inc edx
dec LenI
sub al,bl
jnz _ret
cmp LenI,0
jne _again
_ret: movsx eax,al
ret
uArrayCmp endp


uArrayFind proc uses ebx ecx edx esi edi Where,What,LenI
mov esi,Where
mov edi,What
cmp byte ptr[edi],0
je _ret
cmp LenI,0
je _ret
mov edx,LenI
_again:
mov al,[esi]
mov bl,[edi]
or edx,edx
jz _good
or al,al
jz _ret
inc esi
inc edi
dec edx
.if al!=bl
sub edi,What
sub esi,edi
mov edi,What
inc esi
mov edx,LenI
.endif
jmp _again
_good:
sub edi,What
sub esi,Where
sub esi,edi
mov eax,esi
inc eax
ret
_ret: xor eax,eax
ret
uArrayFind endp



keywords: pattern match regexp regular expressions $1
Posted on 2004-02-04 14:08:37 by Ultrano
Ultrano, You may find the following code of interest: mgrep
Posted on 2004-02-18 19:42:18 by Poimander