README.md

December 5, 2022 ยท View on GitHub

Wildcard Matcher

Description

This code matches a given string against a pattern which may contain the well known wildcards * and ?, whereas * represents any number of characters, including none, and ? represents any single character. You may want to paste the code into a class module :)

More Info

Name as String, Pattern as String. Name is the String you want to match against Pattern.

MatchCase() is the case-sensitive worker function. It makes usage of PreparePattern() to escape the characters # and [ (read Side effects).

Match() is just a wrapper for MatchCase() which does a LCase() on both Name and Pattern parameters.

Returns Boolean True if String matches for Pattern, else returns Boolean False.

This code is written specifically for usage in an IRC bot, thus does escape certain chars which are recognized by the "Like" operator, which this code builds on. Namely, # and [ are taken literally rather than being interpreted as wildcards.

Submitted On
Bygridrun
LevelBeginner
User Rating5.0 (10 globes from 2 users)
CompatibilityVB 6.0
CategoryString Manipulation
WorldVisual Basic
Archive File

Source Code

Public Function Match(Name As String, Pattern As String) As Boolean
  If MatchCase(LCase(Name), LCase(Pattern)) Then Match = True
End Function
Public Function MatchCase(Name As String, Pattern As String) As Boolean
  Pattern = PreparePattern(Pattern)
  If Name Like Pattern Then MatchCase = True
End Function
Private Function PreparePattern(Pattern As String) As String
  Pattern = Replace(Pattern, "[", "[[]")
  Pattern = Replace(Pattern, "#", "[#]")
  PreparePattern = Pattern
End Function