2

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
5
  • What is known in the string? fni.? BTW, which tag is correct: vb.net or vbscript? Please show your code. Commented Jan 28, 2016 at 18:44
  • Just use fni\..*?\\ with the ignore case flag. Commented Jan 28, 2016 at 18:44
  • I don't think I can use fni\..*?\\ , as further through the string the value may reoccur. I'll add the full code now Commented Jan 28, 2016 at 18:49
  • Have a look at fni\.[^\\]*\\. If you use it with a case-insensitive flag, it should work even when multiple values appear in the same string (Global = True with your RegExp is OK then). I will post it. Commented Jan 28, 2016 at 18:54
  • There is no difference between fni\..*?\\ and fni\.[^\]*\\. And @SamH just match the first one. If you're worried about being embedded, just use a whitespace boundary before it, ie (?<!\S)fni\..*?\\ or just a word boundary \b Commented Jan 28, 2016 at 19:09

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Does it work now? Without dism.log file I do not know how to help more.
Yes this query now works. Ontop of the Regex issue I had, I was also accidentally using the test method instead of execute - which certainly wasn't helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.