# RegExp class
{: .no_toc }

Searches strings with a regular expression: finds the matches, tests for one, replaces them and splits a string at them.

**RegExp** is a replacement for the regular expression object of VBScript. It follows the same object model: [**Execute**](/tB/Packages/Contributed/tbRegExp/RegExp/Execute.md) returns a [**MatchCollection**](/tB/Packages/Contributed/tbRegExp/MatchCollection/index.md) of [**Match**](/tB/Packages/Contributed/tbRegExp/Match/index.md) objects, and the indexes of both are zero-based. The matching is done by the vba-regex engine, which the package includes as the **StaticRegex** module.

The class is not predeclared. Create an instance with **New**, set [**Pattern**](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) and the options, then call a method.

```tb check_build check_run
Dim re As RegExp
Set re = New RegExp
re.Pattern = "(\d+)-(\d+)"
re.Global = True

Dim matches As MatchCollection
Set matches = re.Execute("10-20 and 30-40")
Debug.Print matches.Count                        ' 2
Debug.Print matches.Item(0).Value                ' 10-20
Debug.Print matches.Item(1).SubMatches.Item(0)   ' 30

Debug.Print re.Test("none")                      ' False
Debug.Print re.Replace("10-20", "$2-$1")         ' 20-10
```

## Options

Four properties change how the pattern is matched. [**Flags**](/tB/Packages/Contributed/tbRegExp/RegExp/Flags.md) reads and sets all four as one string of letters.

| Property | Flag | When **True** |
|----------|------|---------------|
| [**Global**](/tB/Packages/Contributed/tbRegExp/RegExp/Global.md) | `g` | All matches are used. When **False**, [**Execute**](/tB/Packages/Contributed/tbRegExp/RegExp/Execute.md), [**Replace**](/tB/Packages/Contributed/tbRegExp/RegExp/Replace.md) and [**Split**](/tB/Packages/Contributed/tbRegExp/RegExp/Split.md) use only the first match. |
| [**MultiLine**](/tB/Packages/Contributed/tbRegExp/RegExp/MultiLine.md) | `m` | `^` and `$` also match at the start and end of each line. |
| [**IgnoreCase**](/tB/Packages/Contributed/tbRegExp/RegExp/IgnoreCase.md) | `i` | Letter case is ignored. |
| [**DotAll**](/tB/Packages/Contributed/tbRegExp/RegExp/DotAll.md) | `s` | `.` also matches a line feed. |

All four are **False** in a new instance.

## Invalid patterns

The pattern is compiled the first time a method or one of [**IsValid**](/tB/Packages/Contributed/tbRegExp/RegExp/IsValid.md), [**LastError**](/tB/Packages/Contributed/tbRegExp/RegExp/LastError.md) and [**LastErrorNumber**](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) needs it. It is compiled again after [**Pattern**](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) or [**IgnoreCase**](/tB/Packages/Contributed/tbRegExp/RegExp/IgnoreCase.md) is set to a different value. [**Global**](/tB/Packages/Contributed/tbRegExp/RegExp/Global.md), [**MultiLine**](/tB/Packages/Contributed/tbRegExp/RegExp/MultiLine.md) and [**DotAll**](/tB/Packages/Contributed/tbRegExp/RegExp/DotAll.md) are applied when a match is made and do not cause a new compilation.

A pattern that does not compile does not raise an error at once. [**IsValid**](/tB/Packages/Contributed/tbRegExp/RegExp/IsValid.md) returns **False**, and [**LastError**](/tB/Packages/Contributed/tbRegExp/RegExp/LastError.md) and [**LastErrorNumber**](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) describe the failure. [**Execute**](/tB/Packages/Contributed/tbRegExp/RegExp/Execute.md), [**Test**](/tB/Packages/Contributed/tbRegExp/RegExp/Test.md), [**Replace**](/tB/Packages/Contributed/tbRegExp/RegExp/Replace.md) and [**Split**](/tB/Packages/Contributed/tbRegExp/RegExp/Split.md) then raise that error, with the number in **LastErrorNumber**, the description in **LastError** and the source `RegExp`. When **IsValid**, **LastError** or **LastErrorNumber** is the call that compiles an invalid pattern, it raises nothing and leaves the **Err** object clear.

## Properties

- [DotAll](/tB/Packages/Contributed/tbRegExp/RegExp/DotAll.md) -- returns or sets whether `.` matches a line feed
- [Flags](/tB/Packages/Contributed/tbRegExp/RegExp/Flags.md) -- returns or sets the four options as a string of letters
- [Global](/tB/Packages/Contributed/tbRegExp/RegExp/Global.md) -- returns or sets whether all matches are used
- [IgnoreCase](/tB/Packages/Contributed/tbRegExp/RegExp/IgnoreCase.md) -- returns or sets whether letter case is ignored
- [IsValid](/tB/Packages/Contributed/tbRegExp/RegExp/IsValid.md) -- returns whether the pattern compiles
- [LastError](/tB/Packages/Contributed/tbRegExp/RegExp/LastError.md) -- returns the message of the compile error
- [LastErrorNumber](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) -- returns the number of the compile error
- [MultiLine](/tB/Packages/Contributed/tbRegExp/RegExp/MultiLine.md) -- returns or sets whether `^` and `$` match at line boundaries
- [Pattern](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) -- returns or sets the regular expression
- [SearchStepLimit](/tB/Packages/Contributed/tbRegExp/RegExp/SearchStepLimit.md) -- returns or sets the step budget that stops a runaway search

## Methods

- [Execute](/tB/Packages/Contributed/tbRegExp/RegExp/Execute.md) -- returns the matches in a string
- [Replace](/tB/Packages/Contributed/tbRegExp/RegExp/Replace.md) -- replaces the matches in a string
- [Split](/tB/Packages/Contributed/tbRegExp/RegExp/Split.md) -- splits a string at the matches
- [Test](/tB/Packages/Contributed/tbRegExp/RegExp/Test.md) -- returns whether a string contains a match

## See Also

- [MatchCollection](/tB/Packages/Contributed/tbRegExp/MatchCollection/index.md) class -- the matches that **Execute** returns
- [Match](/tB/Packages/Contributed/tbRegExp/Match/index.md) class -- one match
- [SubMatches](/tB/Packages/Contributed/tbRegExp/SubMatches/index.md) class -- the groups of one match
- [tbRegExp package](/tB/Packages/Contributed/tbRegExp/index.md) -- overview
