
Maybe someone can make use of it where the regular expressions are not required.
In addition to most other simple wildcard functions, this one accepts
'#' as a digit placeholder and '\' as the escape character.
Code: Select all
; Checks if the text matches the specified pattern.
; Supported Wildcards:
; -> * Match everything.
; -> ? Match exactly one character.
; -> # Match a digit.
; -> \ Used to escape '*', '?', '#' and '\'.
; Returns '1' if the text matches the pattern, '0' if not.
; -1 will be returned if the pattern is invalid.
Procedure.i IsWildcardMatch(text.s, pattern.s)
Protected *text.Character
Protected *pattern.Character
Protected *match.Character
Protected *current.Character
*text = @text
*pattern = @pattern
While *text\c <> #Null
Select *pattern\c
Case '\'
*pattern + SizeOf(Character)
Select *pattern\c
Case '*', '?', '#', '\'
If *pattern\c <> *text\c
ProcedureReturn #False
Else
*pattern + SizeOf(Character)
*text + SizeOf(Character)
EndIf
Default
ProcedureReturn -1
EndSelect
Case '*'
*pattern + SizeOf(Character)
If *pattern\c = #Null
ProcedureReturn #True
EndIf
*match = *pattern
*current = *text + SizeOf(Character)
Case '#'
If *text\c < '0' Or *text\c > '9'
ProcedureReturn #False
Else
*text + SizeOf(Character)
*pattern + SizeOf(Character)
EndIf
Case '?', *text\c
*text + SizeOf(Character)
*pattern + SizeOf(Character)
Default
If *current = #Null
ProcedureReturn #False
Else
*pattern = *match
*text = *current
*current + SizeOf(Character)
EndIf
EndSelect
Wend
While *pattern\c = '*'
*pattern + SizeOf(Character)
Wend
If *pattern\c = #Null
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Debug IsWildcardMatch("343", "###")
Debug IsWildcardMatch("Match this Text!", "Match th?? Text!")
Debug IsWildcardMatch("Match that Text!", "Match th?? Text!")
Debug IsWildcardMatch("ERROR: 404", "ERROR: ###")
Debug IsWildcardMatch("Star: * ABC3", "Star: \* ABC#")
Debug IsWildcardMatch("BBB DDD", "B*")
Debug "----------------------------------------------------------------------"
Debug IsWildcardMatch("No Match!", "No Match\?")
Debug IsWildcardMatch("AAABBBCCC", "AAA???DDD")
Debug IsWildcardMatch("BBB DDD", "#")