1

VBAを使用して簡単なスクリプトを作成しました。(Excelでいくつかの作業を最適化する必要があります)。

正規表現に関する最初の質問:

前に言ったように、私はVBAを使用しました。

簡単なタスク:パターンの一致を取得し、サブ一致をキャプチャします。

私のコードは:

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

LASTマッチとLASTサブマッチを取得するにはどうすればよいですか?(長さ-プロパティ?)

Dir関数に関する2番目の質問:

ファイルをフィルタリングするにはどうすればよいですか?

私は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-'msdn-guy'を停止します!冗談ですか?それはただ一つの方法ですか?

この途方もないラインの方法ではなく、通常のフィルターを作成するための可能な方法はありますか?

4

2 に答える 2

3

すべての一致、部分一致、長さなどを取得するには、次のようなものを使用します-例を示すために、より単純なパターンを使用した実用的な例を追加しました(つまり、一連の数値を非数値と一致させます)

Testエラーを回避するために、一致が見つかったと推定する前に正規表現を使用する必要があります

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
于 2012-05-03T10:35:08.170 に答える
1

質問1

あなたはこれを試すことができます:

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

質問2

おそらくdir()、関数は拡張子に基づいてファイルのリストを作成することができません。

[編集]

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

[/編集]

于 2012-05-03T10:30:07.760 に答える