2012-05-03 10 views
1

VBAを使用して簡単なスクリプトを書きました。 (私はいくつかの作業をExcelで最適化する必要があります)。正規表現についてVBA:RegexとDir()関数の質問

最初の質問:

私は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部分マッチを得るのですか? 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でこのコードを見ました!冗談ですか?それは1つの方法だけですか?

ノーマルフィルタを作成する方法はありますか?この方法はありませんか?

+1

(1)あなたに最後の試合を与えるためにテストするためのサンプル文字列がありますか? (2)あなたはここに2つの非常に別々の質問があり、そのように質問されるべきです – brettdj

+0

20.20 22.22 – gaussblurinc

答えて

3

- 私は証明するために単純なパターンで実施例が追加されました(すなわち、その後に非数を数字の配列が一致する)

あなたは試合を想定する前に、正規表現をTestすべきはerros

を避けることが見出されています210
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 
+0

+1うまくいった! –

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 

Quetion 2

はおそらくdir()機能はのベースのファイルのリストを作ることができません拡張。

すべての一致、サブマッチ、あなたはこのようなものを使用することになり、長さなどを取得するには、[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]

+0

素晴らしいです!しかし、もし私がそれを持っていたら、最後のサブマッチはいかがですか? '.Pattern =" (\ d +)\。(\ d +) "です。マッチとサブマッチの両方でインデックスを作成する方法は? – gaussblurinc