Home > Language Reference > Classes
StringMatcher Class
+ Object
+ StringMatcher
Description
The StringMatcher class implements efficient algorithms for finding patterns in strings. It is typically used to find a word or expression in database records, for example in response to the FindItem event. This class is instantiable but not cloneable nor derivable.
The StringMatcher object has two modes of use. In the first, the searched pattern cannot contain any wildcards but the search is extremely fast. The second mode is slower but the searched pattern is a regular expression which can contain wildcards. In both cases, using the object is similar. After having instantiated the object, you should set the search mode by calling the SetPattern or SetRegExp functions. Then you can call the InStr function as many times as necessary to see if the pattern appears in the specified string.
The example below illustrates how to search for the word "keyword" in the Note field of all the records in a database.
Private Sub Button1_Click()
Dim t as New tblCustomer
Dim sm as New StringMatcher
sm.SetPattern "keyword", False, False, True
t.OpenTable hbModeOpenExisting+hbModeReadOnly
While Not t.EOF
If sm.InStr(1,t.Note)>0 Then MsgBox "Keyword found in record " & t.AbsolutePosition
t.MoveNext
Wend
t.Close
End SubYou can also use the StringMatcher object to determine if entered data, for example an email address, is valid. In this case, after having defined the pattern to match with the SetRegExp method, you can use the InStr method and conclude that the test expression is valid if the return value and the value stored in iLength are respectively equal to 1 and the length of the test expression.
Dim sm As New StringMatcher Private Sub Form_Load() sm.SetRegExp "[0-9a-z_]+(-|\\.[0-9a-z_]+)*@[0-9a-z_]+(\\.|-[0-9a-z_]+)+", True, True, True End Sub
Private Sub Button1_Click() Dim n As Integer, s as String s=Field1.Text If sm.InStr(1,s,n) = 1 And n = Len(s) Then Msgbox " Correct Address " Else Msgbox " Incorrect Address " End Sub
Note that the call to SetRegExp, which is an operation costly in both calculation time and memory, is only carried out once.
Using this object is efficient if you need to carry out the same search in several different strings, or if the pattern contains wildcards. If you simply want to test whether a string is contained in another, using the global InStr function is recommended.
Note: in the case of simple pattern searches, the Knuth-Morris-Pratt algorithm is used. In the case of regular expressions, the search uses a deterministic finite state automaton.
Members
| Members | Description |
| InStr | Carries out a search in the specified string. |
| SetPattern | Sets the search pattern. |
| SetRegExp | Sets the regular expression. |
| Inherited from Object | Description |
| ClassID | Returns the type identifier corresponding to the actual class of the object. |
| Implements | Determines whether the object implements the features of a given class. |
| Iterate | Event raised to iterate over the elements of a container object. |
| Recipient | Recipient of events sent by the object. |
| Serialize | Event raised to serialize the object content into a stream. |
System requirements
| System | Minimal version | Remarks |
| Palm OS | Palm OS 3.0 | N/A |