# LastErrorNumber
{: .no_toc }

Returns the number of the error that compiling the current pattern raised.

Syntax: *object*.**LastErrorNumber**

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

Returns a **Long**: the number of the compile error, or 0 when the pattern is valid.

## 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. The compilation traps its error, so **LastErrorNumber** raises nothing. The value is the `Number` of the error that the compilation raised; [**LastError**](/tB/Packages/Contributed/tbRegExp/RegExp/LastError.md) is its description.

The engine raises its pattern errors with numbers of the form `vbObjectError + 3000 + n`, where *n* identifies the fault, as in the example. The compilation traps the error, so **LastErrorNumber** raises nothing and leaves the **Err** object clear.

| *n* | Fault | Example |
|-----|-------|---------|
| 1 | A backslash at the end, a `\c` without a letter, or a `\0` followed by `8` or `9` | `\`, `\c1`, `\08` |
| 2 | A `\x` or `\u` escape without its hexadecimal digits | `\u00` |
| 3 | A range in a character class that runs backwards | `[z-a]` |
| 4 | A character class without its `]` | `[abc` |
| 6 | A group construct `(?<` that is neither a lookbehind nor a valid name | `(?<1a>x)` |
| 8 | A quantifier whose upper count is below its lower count, or a count that is too large | `a{2,1}`, `a{99999999999}` |
| 9 | A group without its `)` | `(\d+` |
| 11 | A `)` that closes no group | `)` |
| 12 | A quantifier with nothing to repeat | `*a` |
| 13 | A group name with a character that a name cannot hold | `(?<a-b>x)` |
| 14 | An inline option that is not `i`, `m` or `s`, which includes an atomic group and a conditional | `(?x)`, `(?>a)` |

An atomic group `(?>...)` and a conditional `(?(`*n*`)`*yes*`|`*no*`)` fall under *n* = 14. VBScript's **RegExp** does not accept either construct, so a pattern that uses one is invalid in both.

[**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 an error with this number when the pattern is invalid.

Those four methods can also raise `vbObjectError + 3016`, which is not a compile error and so never appears here: it means a valid pattern ran out of its step budget before the search finished. See [**SearchStepLimit**](/tB/Packages/Contributed/tbRegExp/RegExp/SearchStepLimit.md).

## Example

```tb check_build check_run
Dim re As RegExp
Set re = New RegExp
re.Pattern = "(\d+)"
Debug.Print re.LastErrorNumber  ' 0

re.Pattern = "(\d+"
Debug.Print re.LastErrorNumber - vbObjectError  ' 3009

On Error Resume Next
re.Test "123"
Debug.Print Err.Number = re.LastErrorNumber  ' True
```

## See Also

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