Reputation: 3682
I wrote a simple script, using VBA. (I need to optimize some work with excel).
First question about Regex:
As I said before, I used VBA.
Simple task: get a match of pattern and capture the submatches.
my code is:
Dim ResStr as Object
Dim LastStr as Object
Dim RE as Object
Set RE = CreateObject("vbscript.regexp") 'create a regex
With RE
.MultiLine = False 'm-key
.Global = False 'g-key
.IgnoreCase = False 'i-key
.Pattern = "[<]TD\s+class=gm[>](\d+\.\d+)[<][/]TD[>]" 'tag
End With
Set ResStr = RE.Execute(StrDollar) 'use regex
Set LastStr = ResStr(0).SubMatches 'get submatch
How do I get the LAST match and LAST submatch? (length-property?)
Second question about Dir function:
How do I filter the files?
I saw this code on msdn:
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
' Display entry only if it's a directory.
Debug.WriteLine(MyName)
End If
MyName = Dir() ' Get next entry.
Loop
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
- stop 'msdn-guy'! are you kidding? is it ONLY one method?
Is there any possible way to make normal filter, not this tremendous-line method?
Upvotes: 1
Views: 2585
Reputation: 11182
Question 1
You could try this:
Sub Test(StrDollar)
Dim LastStr As Object
Dim RE As New RegExp
Dim mt As Match
With RE
.MultiLine = False 'm-key
.Global = True 'g-key
.IgnoreCase = False 'i-key
.Pattern = "<TD\s+class=gm>(\d+\.\d+)</TD>" 'tag
For Each mt In .Execute(StrDollar)
Set LastStr = mt.SubMatches 'get submatch
Next mt
End With
MsgBox LastStr(0)
End Sub
Quetion 2
Perhaps dir()
function is not able to make a list of files based of a extension.
[EDIT]
Sub Test2(StrDollar)
Dim LastStr As Object
Dim RE As New RegExp
Dim mt As Match
Dim dic As New Scripting.Dictionary 'scripting runtime
Dim pos&
With RE
.MultiLine = False 'm-key
.Global = True 'g-key
.IgnoreCase = False 'i-key
.Pattern = "<TD\s+class=gm>(\d+\.\d+)</TD>" 'tag
For Each mt In .Execute(StrDollar)
pos = pos + 1
dic.Add CStr(pos), mt.FirstIndex & "||" & mt.Value & "||" & mt.SubMatches(0)
Next mt
End With
MsgBox dic.item(CStr(pos))
End Sub
[/EDIT]
Upvotes: 1
Reputation: 55682
To get all matches, submatches, length etc you would use something like this - I have added a working example with a simpler pattern to demonstrate (ie match a sequences of numbers with then a non-number)
You should Test
your regexp before presuming a match has been found to avoid erros
Sub Test()
Dim RE As Object
Dim strSample As String
Dim ResStr As Object
Dim LastStr As Object
strSample = "123dd6789a"
Set RE = CreateObject("vbscript.regexp") 'create a regex
With RE
.MultiLine = False 'm-key
.Global = True 'g-key
.IgnoreCase = False 'i-key
.Pattern = "\d+([^\d])"
End With
If RE.Test(strSample) Then
Set ResStr = RE.Execute(strSample)
For Each LastStr In ResStr
MsgBox "Match: " & LastStr & vbNewLine & "SubMatch: " & LastStr.submatches(0) & vbNewLine & "Position: " & LastStr.firstindex + 1 & vbNewLine & "Length: " & LastStr.Length
Next
End If
End Sub
Upvotes: 3