MacSmartypants
REALBasic Pattern Matching Module
Ever wanted to leverage the power of pattern matching in your REALBasic project? Here you can download simple module that provides a function that will take a string and a pattern and tell you if it matches.
If you have ever needed to match a string using a pattern (like in the Microsoft Word search function) this module will allow you to do so. You use wildcard characters as placeholders for other characters when you know only part of the value and/or want to match strings that contain a certain pattern.
Syntax
result = matchstring(text, pattern)
result as boolean: Result tells you if the text you supplied matches the pattern you supplied.
text as string: This is the string you are trying to match.
pattern as string: This is the pattern you are looking for in the text parameter.
Wildcard Usage
* Matches one or more characters. It can be used at the beginning, end or both ends of a pattern but NOT in the middle. Bl* matches blue, black, and blah.
? Matches any single character. R?n matches run, ran, and r1n.
$ Matches any single alphabetical character. R$n matches run, ran but NOT r1n.
# Matches any single numeric character. 2#4 matches 214, 224, 284.
[ ] Matches any single character within the brackets. R[au]n matches ran and run but NOT ron.
! Matches any character not in the brackets. R[!au]n matches ron but NOT ran and run
- Matches any one of a range of characters. You must specify the range in ascending order (A to Z, not Z to A). s[a-c]d matches sad, sbd, scd but NOT sid, and sod.
If you need to match an asterisk (*), question mark (?), dollar sign ($), number sign (#), opening bracket ([), or hyphen (-), enclose the character you are matching in brackets. For example, to match a question mark, use [?]. When matching an exclamation point (!) or closing bracket (]), you do not need to enclose it in brackets. You cannot match opening and closing brackets ([ ]) together because it will be interpreted as an empty string.
[email protected]