[bb] matchPattern by ShadowTurtle [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

Title : matchPattern
Author : ShadowTurtle
Posted : 1+ years ago

Description : compare a source String with a String expression.

** Syntax **
  result = matchPattern(source_string$, expression_pattern$, case_sensitivity)

** Parameters **
source_string
 Target String To compare pattern against.
expression_pattern
 Wildcard expression pattern:
 ? = Any single character Zero Or more characters
 # = Any single digit (0-9)
 case_sensitivity = Toggle for case sensitivity (true Or false).

** Return **
 "True" or "False"


Code :
Code (blitzbasic) Select
; ------------------------------------------------------------------------
; compare a source String with a String expression.
;
; ** Syntax **
;   result = matchPattern(source_string$, expression_pattern$,
;     case_sensitivity)
;
; ** Parameters **
;   source_string            Target String To compare pattern against.
;   expression_pattern       Wildcard expression pattern:
;   ?                        Any single character
;   *                        Zero Or more characters
;   #                        Any single digit (0-9)
;   case_sensitivity         Toggle for case sensitivity (true Or false).
;
; ** Return **
;   "True" or "False"
; ------------------------------------------------------------------------
Function matchPattern(stri$, pattern$, lCase = False)
prevChar$ = ""
nextChar$ = ""

If lCase = True Then
stri$ = stri$ + Chr(0)
pattern$ = pattern$ + Chr(0)
Else
stri$ = Upper(stri$ + Chr(0))
pattern$ = Upper(pattern$ + Chr(0))
End If

retBack = False

x = 1
y = 1

Repeat
If Mid(stri$, x, 1) = Chr(0) Then
If Mid(pattern$, y, 1) = Chr(0) Then
retBack = True
End If
Return retBack
EndIf

If Mid(pattern$, y, 1) = Chr(0) Then
Return retBack
ElseIf Mid(pattern$, y, 1) = Chr(35) Then
x_asc = Asc(Mid(stri$, x, 1))
If x_asc < 48 Or x_asc > 57 Then
Return retBack
End If
ElseIf Mid(pattern$, y, 1) = Chr(42) Then
y = y + 1
Repeat
If Mid(stri$, x, 1) = Mid(pattern$, y, 1) Then
Exit
ElseIf Mid(stri$, x, 1) = Chr(0) Then
Exit
End If
x = x + 1
Forever
If Mid(stri$, x, 1) = Chr(0) Then
If Mid(pattern$, y, 1) = Chr(0) Then
retBack = True
End If
Return retBack
End If
ElseIf Mid(pattern$, y, 1) = Chr(63) Then
; nothing to do, it's a match
Else
If Mid(pattern$, y, 1) <> Mid(stri$, x, 1) Then
Exit
End If
End If

x = x + 1
y = y + 1
Forever
End Function


Comments :


slenkar(Posted 1+ years ago)

 do you have an example?


ShadowTurtle(Posted 1+ years ago)

 
DebugLog matchPattern( "hello, i am a example.", "hello*", True )
DebugLog matchPattern( "hello, i am a example.", "h?ll?*", True )
DebugLog matchPattern( "hallow, i am a exampel.", "h?ll?*examp*", True )
RuntimeError "See debug log window"



slenkar(Posted 1+ years ago)

 thanks quite useful!


BlitzSupport(Posted 1+ years ago)

 Nice, could come in handy.(Interestingly, apart from the comment symbol [;] it works without change in both BlitzMax and Blitz3D/Plus.)


chi(Posted 1+ years ago)

 nice function... thx [/i]