Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA vbscript regular expressions crashes

Tags:

regex

excel

vba

I have a complex VBA project which I have used for ten or fifteen years with no issues. All of sudden, today it started to crash with a non-VBA error message. I am guessing this is referring to the underlying code for the VBScript Regular Expressions 5.5 reference:

MSVCR error dialog: Assertion failed

Microsoft Visual C++ Runtime Library

Assertion failed!

Program: ...
File: g:\vba\src\65_VC8\VBA\rt\regexpbase.cxx
Line: 2063

Expression: ibLim == m_ibMin

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)

[Abort] [Retry] [Ignore]

The following code snippet, at least on my computer, reproduces the problem (this code uses early binding, but the same error occurs using late binding):

Option Explicit
Sub due()
    Dim RE As RegExp
    
Const sPATepi As String = "(?!.*sample)\\([^\\]+)\\(S\d\d(?:E\d{2,3}(?:-E?\d{2,3})?)+)\\(.*)(?=\.(?:mkv|avi|mp4|wmv)$)"
Const Test As String = "\\DS718\Video\TV Shows\Death in Paradise\S14E08\Episode 8 720p HDTV.mkv"

Set RE = New RegExp
With RE
    .Global = True
    .IgnoreCase = True
    .Pattern = sPATepi
    .Multiline = True
End With

'Crashes if result = True
Debug.Print RE.Test(Test)

End Sub

RE.Test(test) in the above code evaluates to True in a regex tester (as it has on this program for years).

Simpler regular expressions do not have this problem. (eg. pattern = "\d+" will evaluate to True and not crash).

I really don't know where to go with debugging this further. I have run DISM with no errors found.

like image 929
Ron Rosenfeld Avatar asked May 22 '26 00:05

Ron Rosenfeld


1 Answers


As of Microsoft® Excel® for Microsoft 365 MSO (Version 2509 Build 16.0.19231.20138) 64-bit (Current Channel Click-to-Run) the bug seems to have been repaired.


I hesitate to post this answer because, even though it works, I'm not sure why it works nor how long it may continue to work in the future.

The problem seems to have arrived with the latest office update (2508) and has been widely reported.

And I would like to thank all of the commenters for their useful and relevant comments, as well as for confirming that this is an issue that has cropped up in multiple environments.

Not sure if this is related, but I note that in VBA, setting of a reference to Microsoft VBScript Regular Expressions 5.5 is no longer necessary.

But it seems as if initiating the regex engine differently restores the functionality of the lookahead both positive and negative.

I obtained this suggestion from another thread on a Microsoft forum in the answer posed by Viorel (NOT the accepted answer). I get ASSERTION FAILED using VBSCRIP RegEx

Instead of declaring as

Dim oRegEx As RegExp
Set oRegEx = New RegExp

We declare as:

Dim oRegEx As RegExp
Set oRegEx = GetObject("", "VBScript.RegExp")

Why this works is unclear to me.

like image 83
Ron Rosenfeld Avatar answered May 23 '26 21:05

Ron Rosenfeld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!