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.
Although nothing is raised, the compile error of an invalid pattern stays in the Err object after the call that compiled the pattern returns. A program that tests Err for other errors calls Err.Clear first.
Note
A quantifier whose lower count is above its upper count, such as a{2,1}, is not found when the pattern is compiled, so IsValid returns True for it. Execute, Test, Replace and Split then raise error 3000 when a search reaches the quantifier, with the description that Windows gives that number, The specified print monitor is unknown. This is how version 1.7.0.0 behaves.
Example
Dim re As RegExp
Set re = New RegExp
re.Pattern = "(\d+"
Debug.Print re.IsValid ' False
Debug.Print Err.Number = re.LastErrorNumber ' True
Err.Clear
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