Jump to content

VbScript RegExp Question


Recommended Posts

hi,

Could someone help me discover the RegExp for KB1234567.exe

I want to check if the first 2 chars are "KB", the following 6 chars are between [0-9] and the last four are ".exe".

I only discovery how to check in the beginning or in the end, i don't know how to make the 3 conditions.

Sorry my english,

thanks

Link to comment
Share on other sites


You could use a inStr to filter out what you want

Example Filter Out One String Element

Save As InstrDemo_1.vbs

Dim MyArray, StrArray, Report

'/-> Array Of Names
MyArray = Array("ZZ_SomeApp.exe", "Unknown.Dll","FolderApp.exe", "NewPaintApp.exe", "KB1234567.exe",_
"SomeThing.Dll", "CoolApp.exe", "TheTextDoc.txt")

'/-> For Each Loop For The Array MyArray
For Each StrArray In MyArray
If InStr(StrArray,"KB") Then '-> Filter Out KB In The Varible StrArray
Report = Report & vbCrLf &_
" --------------------------------------------------------------------------- " & vbcrlf &_
"Correct File " & vbTab & Chr(187) & Space(3) & StrArray & vbCrLf &_
" --------------------------------------------------------------------------- "
Else
Report = Report & vbCrLf & "Wrong File " & vbTab & Chr(187) & Space(3) & StrArray
End If
Next

'/-> Report The Correct And Wrong
MsgBox Report, 0 + 32 + 4096, "InStr Report"

Example Filter Out Two String Element

Save As InstrDemo_2.vbs

Dim MyArray, StrArray, Report

'/-> Array Of Names
MyArray = Array("ZZ_SomeApp.exe", "Unknown.Dll","FolderApp.exe", "NewPaintApp.exe", "KB1234567.exe",_
"SomeThing.Dll", "CoolApp.exe", "TheTextDoc.txt")

'/-> For Each Loop For The Array MyArray
For Each StrArray In MyArray
If InStr(StrArray,".exe") Then '-> Filter Out .exe In The Varible StrArray
If InStr(StrArray,"KB") Then '-> Filter Out KB In The Varible StrArray
Report = Report & vbCrLf &_
" --------------------------------------------------------------------------- " & vbcrlf &_
"Correct File " & vbTab & Chr(187) & Space(3) & StrArray & vbCrLf &_
" --------------------------------------------------------------------------- "
Else
Report = Report & vbCrLf & "Wrong File " & vbTab & Chr(187) & Space(3) & StrArray
End If
End If
Next

'/-> Report The Correct And Wrong
MsgBox Report, 0 + 32 + 4096, "InStr Report"

Edited by gunsmokingman
Link to comment
Share on other sites

B)

Const test = "KB1234567.exe"

Dim strFileName
Dim lngKbNumber

strFileName = test

If UCase(Left(strFileName,2)) = "KB" Then

If UCase(Right(strFileName,4)) = ".EXE" Then

On Error Resume Next
lngKbNumber = CLng(Mid(strFileName,3,InStrRev(strFileName,".")-2))
On Error Goto 0

If Err = 0 Then

'Your code here

End If

End If

End If

Link to comment
Share on other sites

What have you got so far? and will either of these help!

KB\d{6}\.exe

or

KB[0-9]{6}\.exe

thanks to all for the examples, this is what i was looking for ("kb[0-9]{6}.exe").

I was using complex syntax and vbscript also uses simple... like this one.

Dim objRegExp

Set objRegExp = New RegExp

objRegExp.Pattern = "kb[0-9]{6}.exe"

objRegExp.IgnoreCase = False

Dim strVariable

'strVariable = "kb987653.exe"

strVariable = "kb98763.exe"

If objRegExp.Test( strVariable ) Then

wscript.echo "Correct File"

else

wscript.echo "InCorrect File"

End If

Link to comment
Share on other sites

I still think that you should be using the syntax I gave

KB[0-9]{6}\.exe

the 'backslash' escapes the period, (which matches any printable or nonprintable character). Your current implementation should technically match KB123456jexe. I know that filename is not a likely one to come accross however it is still a possibility.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...