VSFlexString Introduction > Matching Demo |
This demo shows one of the simplest uses for the VSFlexString control: retrieving information from text based on patterns.
Suppose we have a text file containing client names and phone numbers, and want to retrieve the names of all clients who are in the 415 area code.
Here is what the list looks like:
Example Title |
Copy Code
|
---|---|
ClientList = "John Doe: (415) 555-1212," & _ "Mary Smith: (212) 555-1212," & _ "Dick Tracy: (412) 555-1212," & _ "Martin Long: (415) 555-1212," & _ "Leo Getz: (510) 555-1212," & _ "Homer Simpson: (415) 555-1212" |
The most important part of the task is developing the Pattern string. It would definitely contain the string "(415)", which is our criterion for filtering the data. But using "(415)" would produce a number of matches consisting of the string "(415)", which is not very useful. So we extend the pattern to include everything before and after the "(415)" up to the record delimiter, in this case a comma. The Pattern string would look like this: "[^,]*(415)[^,]*".
Here is the code (fs is a VSFlexString control):
Example Title |
Copy Code
|
---|---|
fs.Text = ClientList fs.Pattern = "[^,]*(415)[^,]*" Debug.Print fs.MatchCount " match(es) found." For i = 0 to fs.MatchCount - 1 Debug.Print "found: ["; fs.MatchString(i); "]" Next And here is the result: found: [John Doe: (415) 555-1212] found: [Martin Long: (415) 555-1212] found: [Homer Simpson: (415) 555-1212] |
You could easily extend the Pattern string to take other delimiters into account. For example, to recognize commas, tabs, and line breaks you would use "[^,\t\n\l]*(415)[^,\t\n\l]*".