The VSFlexString control allows you to incorporate regular-expression text matching into your applications. This allows you to parse complex text easily, or to offer regular expression search-and-replace features such as those found in professional packages like Microsoft Word, Visual C++, and Visual Basic.
VSFlexString looks for text patterns on its Text property, and lets you inspect and change the matches it finds. The text patterns are specified through the Pattern property, using a regular expression syntax similar to the ones used in Unix systems.
The usual sequence of steps involved in using the VSFlexString controls is this:
1. Assign the string containing the text you want to work on to the Text property.
2. Assign a string containing a regular expression to the Pattern property. At this point, the VSFlexString control will automatically find all the matches it can.
3. Loop through the matches found, from zero to MatchIndex - 1.
4. For each match, perform some operation using the MatchString, MatchStart, and MatchLength properties.
For example, suppose you want to scan an HTML string and locate all the HTML tags in it. A good pattern to use in this case would be the string "<[^>]*>", which means "a less-than sign, followed by any number of characters different than a greater-than sign, followed by a greater-than sign". The syntax for the regular expressions that describe patterns is explained in detail in later sections.
After assigning the HTML text to the Text property and the "<[^>]*>" string to the Pattern property, the VSFlexString control will automatically find as many matches as it can, and expose them to your application through the Match* properties. The picture below illustrates this:
The picture shows the Text and Pattern properties. Below the strings, you can see the six matches that were found (the number of matches found is returned in the MatchCount property). The MatchString property returns an array of strings, indexed from zero to MatchCount - 1, containing the text in each match. The MatchStart and MatchLength properties return the position and length of each match.
From this point on, you can work on the string either by modifying it directly or by changing the text in the TagString property.
The VSFlexString control extends the concept of matching patterns by allowing you to specify tags in each pattern. The tags are created by enclosing parts of the Pattern string in curly brackets ("{}"). This is useful when the pattern contains parts that need special processing.
For example, still using the same HTML text, imagine you want to extract all text that is bold or italic. You could use a pattern such as "<[BI]>{[^>]*}</[BI]>", which means "a less-than sign followed by a B or an I, followed by a greater-than sign, followed by any number of characters different than a greater-than sign, followed by a less-than sign, followed by a slash, followed by either a B or an I, followed by a greater-than sign". The important thing to notice here is that the actual text (excluding the tags) is enclosed in curly brackets and is thus recognized by the VSFlexString control as a tag. Tags can be accessed using the TagString, TagStart, and TagLength properties. A pattern may contain any number of tags.
The picture below shows how the tags would be retrieved.
Note that the tag index is relative to the current match. Thus, the first tag ("easy") would be retrieved by setting MatchIndex to zero and TagIndex to zero. The second tag ("Pattern Matching") would be retrieved by setting MatchIndex to one and TagIndex to zero. You would set TagIndex to a number greater than zero only if your pattern contained multiple tags.
The most important aspect of learning how to use the VSFlexString control is understanding and becoming familiar with the regular expression syntax. The following topics describe VSFlexString Regular Expressions and present a few possible uses for the control.