# IsValid
{: .no_toc }

Returns whether the current pattern compiles. Raises no error.

Syntax: *object*.**IsValid**

*object*
: *required* An object expression that evaluates to a **RegExp**.

Returns a **Boolean**: **True** when [**Pattern**](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) compiles, **False** when it does not.

## Remarks

The property compiles the pattern if it has not been compiled since [**Pattern**](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) or [**IgnoreCase**](/tB/Packages/Contributed/tbRegExp/RegExp/IgnoreCase.md) last changed, and keeps the result. Nothing is raised when the compilation fails. For a pattern that fails, [**LastError**](/tB/Packages/Contributed/tbRegExp/RegExp/LastError.md) holds the message and [**LastErrorNumber**](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) the number.

Check **IsValid** before [**Execute**](/tB/Packages/Contributed/tbRegExp/RegExp/Execute.md), [**Test**](/tB/Packages/Contributed/tbRegExp/RegExp/Test.md), [**Replace**](/tB/Packages/Contributed/tbRegExp/RegExp/Replace.md) or [**Split**](/tB/Packages/Contributed/tbRegExp/RegExp/Split.md) when the pattern comes from outside the program, because each of those raises the compile error.

Nothing is raised and nothing is left behind. The **Err** object is clear when the property returns, even for a pattern that failed to compile, so a program that runs `On Error Resume Next` and tests **Err** for its own errors does not have to call **Err.Clear** first. The number and the message are kept in [**LastErrorNumber**](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) and [**LastError**](/tB/Packages/Contributed/tbRegExp/RegExp/LastError.md).

A quantifier whose lower count is above its upper count, such as `a{2,1}`, is found when the pattern is compiled, like any other fault: **IsValid** returns **False**, [**LastErrorNumber**](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) gives the invalid-quantifier number, and [**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) raise it. A `{` that starts no quantifier at all, such as the one in `a{x}`, is not a fault but an ordinary character.

## Example

```tb check_build check_run
Dim re As RegExp
Set re = New RegExp
re.Pattern = "(\d+"
Debug.Print re.IsValid                          ' False
Debug.Print Err.Number                          ' 0 -- nothing left in Err
Debug.Print re.LastErrorNumber - vbObjectError  ' 3009

re.Pattern = "(\d+)"
Debug.Print re.IsValid  ' True
```

## See Also

- [LastError](/tB/Packages/Contributed/tbRegExp/RegExp/LastError.md) property -- the message of the compile error
- [LastErrorNumber](/tB/Packages/Contributed/tbRegExp/RegExp/LastErrorNumber.md) property -- the number of the compile error
- [Pattern](/tB/Packages/Contributed/tbRegExp/RegExp/Pattern.md) property -- the regular expression
- [RegExp](/tB/Packages/Contributed/tbRegExp/RegExp/index.md) class -- overview
