Sam H
Sam H

Reputation: 362

VBScript RegEx not matching

I'm slightly confused as to how RegExp works. I'm very happy using it to search for strings using something like notepad++, however I'm now trying to search through a string in VBScript and the RegExps that work in notepad++ don't seem compatible with VBScript. I incorrectly presumed that regexp was a standard of sorts. Anyway.

The string I'm trying to search through is:
"kcabllaCsrevirD - ) 0x0 = TLUSERH ( 'FNI.TRWTS\049BE47424A6-2898-65A4-3538-602212F0\#\$1#40C0010B$gkPrevirD_O_\10RPAFJUOS\\' egakcaP revirD 8202=DI

I'm trying to identify:
FNI.TRWTS\

Using notepad++, and trying to follow the sytax as described in this MSDN article I've come up with:
.*?fni\..*?\\

Can anyone point me in the right direction here? I have other regexps working in VB so am fairly happy that my VB is OK.

For some background on the string - I've reversed a line of text from a DISM log and am trying to extract the driver name, so looking to pick out fni.* and then reverse it back to *.inf. The reason I'm doing it this way is whilst I can get regexps to search non greedy (.*?) I can't seem to find a method of matching last first.

Set objFso = CreateObject("Scripting.FileSystemObject")
Set TxtDismLog = objFSO.GetFile("C:\Windows\SysWow64\CCM\Logs\dism.log").OpenAsTextStream(1,-2) 
Set TxtDriverOutput = objFSO.CreateTextFile("C:\Program Files\Sam\drivers.log", 8, True)
Set objRegEx = CreateObject("VBScript.RegExp")
Set objRegEx2 = CreateObject("VBScript.RegExp")

objRegEx.Global = True   
objRegEx.IgnoreCase = True
objRegEx.Pattern = "Found \d driver package"

objRegEx2.Global = True   
objRegEx2.IgnoreCase = True
objRegEx2.Pattern = "\bfni\.[^\\]*\\"

txtDriverOutput.WriteLine Now() & Chr(32) & "Begin DISM Driver Scan"
Do While TxtDismLog.AtEndOfStream <> True 
txtline = txtDismLog.ReadLine
If objRegEx.Test(txtline) Then
h = InStr(txtline,"Found")
i = Mid(txtline,h+6,1)
Do While i <> 0
i = i - 1
txtline = txtDismLog.ReadLine
txtlinebwd = StrReverse(txtline)
regfindbwd = objRegEx2.Execute(txtlinebwd)
regfind = StrReverse(regfindbwd)
txtDriverOutput.WriteLine regfind
Loop
End If
Loop

Upvotes: 2

Views: 1112

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

Note that Notepad++ uses Boost regex library which is very powerful, and VBScript uses a very old regex library similar to what JavaScript supports (it is very limited compared to Boost). However, very basic patterns will work the same.

To match a substring starting with fni. and ending with \, you can use

\bfni\.[^\\]*\\

See regex demo

The \b forces fni to be a whole word. [^\\] matches any character but a \, * matches zero or more occurrences, and \\ matches one \.

The RegExp.Execute returns all matches if you set objRegEx2.Global = True, so there is no point setting a loop.

Upvotes: 2

Related Questions