Regular Expressions

The regular expression syntax recognized by VSFlexString is based on the following special characters:

 

Char

Description

^

Beginning of a string.

$

End of a string.

.

Any character.

[list]

Any character in list. For example, "[AEIOU]" matches any single uppercase vowel.

[^list]

Any character not in list. For example, "[^ ]" matches any character except a space.

[A-Z]

Any character between 'A' and 'Z'. For example, "[0-9]" matches any single digit.

?

Repeat previous character zero or one time. For example, "10?" matches "1" and "10".

*

Repeat previous character zero or more times. For example, "10*" matches "1", "10", "1000", and so on.

+

Repeat previous character one or more times. For example, "10+" matches "10", "1000", and so on.

\

Escape next character. This is required to any of the special characters that are part of the syntax. For example "\.\*\+\\" matches ".*+\". It is also required to encode some special non-printable characters (such as tabs) listed below.

{tag}

Tag this part of the match so you can refer to it later using the TagString property.

 

In addition to the characters listed above, there are seven special characters encoded using the backslash. These are listed below:

 

Code

Description

\a

Bell (Chr(7))

\b

Backspace (Chr(8))

\f

Formfeed (Chr(12))

\n

New line (Chr(10), vbLf)

\r

Carriage return (Chr(13), vbCr)

\t

Horizontal tab (Chr(9), vbTab)

\v

Vertical tab (Chr(11))

 

For example,

    "^stuff"                 ' any string starting with "stuff"

    "stuff$"                 ' any string ending with "stuff"

    "o.d"                    ' "old", "odd", "ord", and so on

    "o[ld]d"                 ' "old" or "odd" only

    "o[^l]d"                 ' "odd", "ord", but not "old"

    "od?"                    ' "o" or "od"

    "od*"                    ' "o", "od", "odd"

    "od+"                    ' "od", "odd", and so on

    "[A-Z][a-z]*"            ' any uppercase word

    "[0-9]+"                 ' any stream of digits

    "\."                     ' decimal point (needs escape

                             ' character)

 

    "[1-9]+[1-9]*"           ' any stream of digits not

                             ' starting with 0

 

    "[+\-]?[0-9]*[\.]?[0-9]*" ' any number with optional sign

                              ' and decimal point

                              ' (needs two escape characters)

One of the best ways to develop and quickly test your patterns is using the Visual Basic Property window. If you place a VSFlexString control on a form, then click on it and press F4, the Property window appears. You can then type directly into the Text and Pattern properties and watch the MatchCount property change. For example, if you type "[a-z]" into the Pattern property and "Hello World" into the Text property, MatchCount will be set to 8 (the number of lower case character in the text). If you change the Pattern to "[A-Za-z]*", the MatchCount property will change to 2 (the number of words in the text).

Note that if a pattern can match the string in more than one way, the longest match will prevail. For example,

    fs.Text = "testing, 1, 2, 3. Done testing."

    fs.Pattern = ".*,"

    Debug.Print fs.MatchCount; "["; fs.MatchString; "]"

This pattern means "any sequence of characters terminating in a comma". It is ambiguous, because the control could break up the string in the following ways:

     testing, 1, 2, 3. Done testing.     (Three matches)

     testing, 1, 2, 3. Done testing.     (Two matches)

     testing, 1, 2, 3. Done testing.     (Two matches)

     testing, 1, 2, 3. Done testing.     (One match)

In such cases, the control will extend the match as far as it can, thus finding the longest match. The output will be:

     1 [testing, 1, 2,]