IsValid

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 compiles, False when it does not.

Remarks

The property compiles the pattern if it has not been compiled since Pattern or IgnoreCase last changed, and keeps the result. Nothing is raised when the compilation fails. For a pattern that fails, LastError holds the message and LastErrorNumber the number.

Check IsValid before Execute, Test, Replace or Split 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 and LastError.

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 gives the invalid-quantifier number, and Execute, Test, Replace and Split 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

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 property – the message of the compile error
  • LastErrorNumber property – the number of the compile error
  • Pattern property – the regular expression
  • RegExp class – overview