tbRegExp Package

Author: GCuser99. On GitHub: GCuser99/tbRegExp, in the folder Package.

The tbRegExp contributed package provides regular expressions for twinBASIC projects, as a replacement for the RegExp object of VBScript. It follows the same object model. A RegExp is given a pattern and options, its Execute method returns a MatchCollection of Match objects, and each Match holds the text captured by the groups of the pattern as a SubMatches object. Positions are zero-based, as in VBScript.

The matching is done by the vba-regex engine of sihlfall, which the package includes as a private module, StaticRegex.

Beyond VBScript’s object, RegExp has the DotAll and Flags options, the Split method, and IsValid, LastError and LastErrorNumber, which report an invalid pattern before it is used. Replace reads a $ in the replacement string differently from VBScript.

Pattern syntax

A pattern is written as for VBScript’s RegExp. tbRegExp also accepts:

  • named groups, (?<name>...): SubMatches.Item returns one by its name, and Replace inserts one as $<name>;
  • lookbehind, (?<=...) and (?<!...), as well as lookahead;
  • the options i, m and s inside the pattern: (?i) for the rest of the pattern, (?i:...) for one group.
Dim re As New RegExp
re.Pattern = "(?<year>\d{4})-(?<month>\d{2})"
Debug.Print re.Execute("2026-10")(0).SubMatches("month")  ' 10
Debug.Print re.Replace("2026-10", "$<month>/$<year>")     ' 10/2026

re.Pattern = "(?<=\$)\d+"
Debug.Print re.Execute("costs $123")(0).Value            ' 123

re.Pattern = "(?i:abc)DEF"
Debug.Print re.Test("ABCDEF")                            ' True
Debug.Print re.Test("ABCdef")                            ' False

A pattern that holds \k<name>, \A, \Z, \p{...}, an atomic group (?>...) or a conditional is not valid: IsValid returns False. A numbered backreference such as \1 works. \w matches only the ASCII letters and digits and _, as in VBScript, so \w+ finds caf in café.

Classes

  • Match – holds one match that a RegExp found in a string: the matched text, where it starts, how many characters it has, and the text captured by its groups
    • FirstIndex – returns the zero-based position where the match starts
    • Length – returns the number of characters in the match
    • SubMatches – returns the text captured by the groups of the pattern
    • Value – returns the matched text (default member)
  • MatchCollection – holds the Match objects that a regular expression finds in a string
    • Count – returns the number of matches
    • Item – returns one match by position (the default member)
    • NewEnum – returns the enumerator that For Each uses (hidden)
  • RegExp – searches strings with a regular expression: finds the matches, tests for one, replaces them and splits a string at them
    • DotAll – returns or sets whether . matches line terminators
    • Execute – returns the matches in a string
    • Flags – returns or sets the four options as a string of letters
    • Global – returns or sets whether all matches are used
    • IgnoreCase – returns or sets whether letter case is ignored
    • IsValid – returns whether the pattern compiles
    • LastError – returns the message of the compile error
    • LastErrorNumber – returns the number of the compile error
    • MultiLine – returns or sets whether ^ and $ match at line boundaries
    • Pattern – returns or sets the regular expression
    • Replace – replaces the matches in a string
    • Split – splits a string at the matches
    • Test – returns whether a string contains a match
  • SubMatches – holds the strings that the capture groups of a pattern captured in one Match
    • Count – returns the number of strings
    • Item – returns one string by position or by group name (the default member)
    • NewEnum – returns the enumerator that For Each uses (hidden)